Commit 46c0c0f4 authored by kasperl@chromium.org's avatar kasperl@chromium.org

Optimize 'typeof <expr> == <string literal>' in the full codegen.

Review URL: http://codereview.chromium.org/3110034

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5326 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent b1e27e2c
...@@ -2632,30 +2632,7 @@ void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) { ...@@ -2632,30 +2632,7 @@ void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
case Token::TYPEOF: { case Token::TYPEOF: {
Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)"); Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
VariableProxy* proxy = expr->expression()->AsVariableProxy(); VisitForTypeofValue(expr->expression(), kStack);
if (proxy != NULL &&
!proxy->var()->is_this() &&
proxy->var()->is_global()) {
Comment cmnt(masm_, "Global variable");
__ ldr(r0, CodeGenerator::GlobalObject());
__ mov(r2, Operand(proxy->name()));
Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
// Use a regular load, not a contextual load, to avoid a reference
// error.
__ Call(ic, RelocInfo::CODE_TARGET);
__ push(r0);
} else if (proxy != NULL &&
proxy->var()->slot() != NULL &&
proxy->var()->slot()->type() == Slot::LOOKUP) {
__ mov(r0, Operand(proxy->name()));
__ Push(cp, r0);
__ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
__ push(r0);
} else {
// This expression cannot throw a reference error at the top level.
VisitForValue(expr->expression(), kStack);
}
__ CallRuntime(Runtime::kTypeof, 1); __ CallRuntime(Runtime::kTypeof, 1);
Apply(context_, r0); Apply(context_, r0);
break; break;
...@@ -2876,27 +2853,120 @@ void FullCodeGenerator::VisitCountOperation(CountOperation* expr) { ...@@ -2876,27 +2853,120 @@ void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
} }
void FullCodeGenerator::EmitNullCompare(bool strict, void FullCodeGenerator::VisitForTypeofValue(Expression* expr, Location where) {
Register obj, VariableProxy* proxy = expr->AsVariableProxy();
Register null_const, if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
Label* if_true, Comment cmnt(masm_, "Global variable");
Label* if_false, __ ldr(r0, CodeGenerator::GlobalObject());
Register scratch) { __ mov(r2, Operand(proxy->name()));
__ cmp(obj, null_const); Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
if (strict) { // Use a regular load, not a contextual load, to avoid a reference
Split(eq, if_true, if_false, NULL); // error.
__ Call(ic, RelocInfo::CODE_TARGET);
if (where == kStack) __ push(r0);
} else if (proxy != NULL &&
proxy->var()->slot() != NULL &&
proxy->var()->slot()->type() == Slot::LOOKUP) {
__ mov(r0, Operand(proxy->name()));
__ Push(cp, r0);
__ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
if (where == kStack) __ push(r0);
} else { } else {
// This expression cannot throw a reference error at the top level.
VisitForValue(expr, where);
}
}
bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
Expression* left,
Expression* right,
Label* if_true,
Label* if_false,
Label* fall_through) {
if (op != Token::EQ && op != Token::EQ_STRICT) return false;
// Check for the pattern: typeof <expression> == <string literal>.
Literal* right_literal = right->AsLiteral();
if (right_literal == NULL) return false;
Handle<Object> right_literal_value = right_literal->handle();
if (!right_literal_value->IsString()) return false;
UnaryOperation* left_unary = left->AsUnaryOperation();
if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
Handle<String> check = Handle<String>::cast(right_literal_value);
VisitForTypeofValue(left_unary->expression(), kAccumulator);
if (check->Equals(Heap::number_symbol())) {
__ tst(r0, Operand(kSmiTagMask));
__ b(eq, if_true); __ b(eq, if_true);
__ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
__ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
__ cmp(r0, ip);
Split(eq, if_true, if_false, fall_through);
} else if (check->Equals(Heap::string_symbol())) {
__ tst(r0, Operand(kSmiTagMask));
__ b(eq, if_false);
// Check for undetectable objects => false.
__ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
__ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
__ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
__ cmp(r1, Operand(1 << Map::kIsUndetectable));
__ b(eq, if_false);
__ ldrb(r1, FieldMemOperand(r0, Map::kInstanceTypeOffset));
__ cmp(r1, Operand(FIRST_NONSTRING_TYPE));
Split(lt, if_true, if_false, fall_through);
} else if (check->Equals(Heap::boolean_symbol())) {
__ LoadRoot(ip, Heap::kTrueValueRootIndex);
__ cmp(r0, ip);
__ b(eq, if_true);
__ LoadRoot(ip, Heap::kFalseValueRootIndex);
__ cmp(r0, ip);
Split(eq, if_true, if_false, fall_through);
} else if (check->Equals(Heap::undefined_symbol())) {
__ LoadRoot(ip, Heap::kUndefinedValueRootIndex); __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
__ cmp(obj, ip); __ cmp(r0, ip);
__ b(eq, if_true); __ b(eq, if_true);
__ BranchOnSmi(obj, if_false); __ tst(r0, Operand(kSmiTagMask));
// It can be an undetectable object. __ b(eq, if_false);
__ ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset)); // Check for undetectable objects => true.
__ ldrb(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset)); __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
__ tst(scratch, Operand(1 << Map::kIsUndetectable)); __ ldrb(r1, FieldMemOperand(r0, Map::kBitFieldOffset));
Split(ne, if_true, if_false, NULL); __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
__ cmp(r1, Operand(1 << Map::kIsUndetectable));
Split(eq, if_true, if_false, fall_through);
} else if (check->Equals(Heap::function_symbol())) {
__ tst(r0, Operand(kSmiTagMask));
__ b(eq, if_false);
__ CompareObjectType(r0, r1, r0, JS_FUNCTION_TYPE);
__ b(eq, if_true);
// Regular expressions => 'function' (they are callable).
__ CompareInstanceType(r1, r0, JS_REGEXP_TYPE);
Split(eq, if_true, if_false, fall_through);
} else if (check->Equals(Heap::object_symbol())) {
__ tst(r0, Operand(kSmiTagMask));
__ b(eq, if_false);
__ LoadRoot(ip, Heap::kNullValueRootIndex);
__ cmp(r0, ip);
__ b(eq, if_true);
// Regular expressions => 'function', not 'object'.
__ CompareObjectType(r0, r1, r0, JS_REGEXP_TYPE);
__ b(eq, if_false);
// Check for undetectable objects => false.
__ ldrb(r0, FieldMemOperand(r1, Map::kBitFieldOffset));
__ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
__ cmp(r0, Operand(1 << Map::kIsUndetectable));
__ b(eq, if_false);
// Check for JS objects => true.
__ ldrb(r0, FieldMemOperand(r1, Map::kInstanceTypeOffset));
__ cmp(r0, Operand(FIRST_JS_OBJECT_TYPE));
__ b(lt, if_false);
__ cmp(r0, Operand(LAST_JS_OBJECT_TYPE));
Split(le, if_true, if_false, fall_through);
} else {
if (if_false != fall_through) __ jmp(if_false);
} }
return true;
} }
...@@ -2911,6 +2981,16 @@ void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) { ...@@ -2911,6 +2981,16 @@ void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
Label* if_false = NULL; Label* if_false = NULL;
PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false); PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
// First we try a fast inlined version of the compare when one of
// the operands is a literal.
Token::Value op = expr->op();
Expression* left = expr->left();
Expression* right = expr->right();
if (TryLiteralCompare(op, left, right, if_true, if_false, NULL)) {
Apply(context_, if_true, if_false);
return;
}
VisitForValue(expr->left(), kStack); VisitForValue(expr->left(), kStack);
switch (expr->op()) { switch (expr->op()) {
case Token::IN: case Token::IN:
...@@ -2939,24 +3019,10 @@ void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) { ...@@ -2939,24 +3019,10 @@ void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
case Token::EQ_STRICT: case Token::EQ_STRICT:
strict = true; strict = true;
// Fall through // Fall through
case Token::EQ: { case Token::EQ:
cc = eq; cc = eq;
__ pop(r1); __ pop(r1);
// If either operand is constant null we do a fast compare
// against null.
Literal* right_literal = expr->right()->AsLiteral();
Literal* left_literal = expr->left()->AsLiteral();
if (right_literal != NULL && right_literal->handle()->IsNull()) {
EmitNullCompare(strict, r1, r0, if_true, if_false, r2);
Apply(context_, if_true, if_false);
return;
} else if (left_literal != NULL && left_literal->handle()->IsNull()) {
EmitNullCompare(strict, r0, r1, if_true, if_false, r2);
Apply(context_, if_true, if_false);
return;
}
break; break;
}
case Token::LT: case Token::LT:
cc = lt; cc = lt;
__ pop(r1); __ pop(r1);
......
...@@ -323,6 +323,16 @@ class FullCodeGenerator: public AstVisitor { ...@@ -323,6 +323,16 @@ class FullCodeGenerator: public AstVisitor {
void VisitDeclarations(ZoneList<Declaration*>* declarations); void VisitDeclarations(ZoneList<Declaration*>* declarations);
void DeclareGlobals(Handle<FixedArray> pairs); void DeclareGlobals(Handle<FixedArray> pairs);
// Try to perform a comparison as a fast inlined literal compare if
// the operands allow it. Returns true if the compare operations
// has been matched and all code generated; false otherwise.
bool TryLiteralCompare(Token::Value op,
Expression* left,
Expression* right,
Label* if_true,
Label* if_false,
Label* fall_through);
// Platform-specific code for a variable, constant, or function // Platform-specific code for a variable, constant, or function
// declaration. Functions have an initial value. // declaration. Functions have an initial value.
void EmitDeclaration(Variable* variable, void EmitDeclaration(Variable* variable,
...@@ -386,14 +396,6 @@ class FullCodeGenerator: public AstVisitor { ...@@ -386,14 +396,6 @@ class FullCodeGenerator: public AstVisitor {
// accumulator. // accumulator.
void EmitKeyedPropertyAssignment(Assignment* expr); void EmitKeyedPropertyAssignment(Assignment* expr);
// Helper for compare operations. Expects the null-value in a register.
void EmitNullCompare(bool strict,
Register obj,
Register null_const,
Label* if_true,
Label* if_false,
Register scratch);
void SetFunctionPosition(FunctionLiteral* fun); void SetFunctionPosition(FunctionLiteral* fun);
void SetReturnPosition(FunctionLiteral* fun); void SetReturnPosition(FunctionLiteral* fun);
void SetStatementPosition(Statement* stmt); void SetStatementPosition(Statement* stmt);
...@@ -437,6 +439,9 @@ class FullCodeGenerator: public AstVisitor { ...@@ -437,6 +439,9 @@ class FullCodeGenerator: public AstVisitor {
#undef DECLARE_VISIT #undef DECLARE_VISIT
// Handles the shortcutted logical binary operations in VisitBinaryOperation. // Handles the shortcutted logical binary operations in VisitBinaryOperation.
void EmitLogicalOperation(BinaryOperation* expr); void EmitLogicalOperation(BinaryOperation* expr);
void VisitForTypeofValue(Expression* expr, Location where);
void VisitLogicalForValue(Expression* expr, void VisitLogicalForValue(Expression* expr,
Token::Value op, Token::Value op,
Location where, Location where,
......
...@@ -2631,30 +2631,7 @@ void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) { ...@@ -2631,30 +2631,7 @@ void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
case Token::TYPEOF: { case Token::TYPEOF: {
Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)"); Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
VariableProxy* proxy = expr->expression()->AsVariableProxy(); VisitForTypeofValue(expr->expression(), kStack);
if (proxy != NULL &&
!proxy->var()->is_this() &&
proxy->var()->is_global()) {
Comment cmnt(masm_, "Global variable");
__ mov(eax, CodeGenerator::GlobalObject());
__ mov(ecx, Immediate(proxy->name()));
Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
// Use a regular load, not a contextual load, to avoid a reference
// error.
__ call(ic, RelocInfo::CODE_TARGET);
__ push(eax);
} else if (proxy != NULL &&
proxy->var()->slot() != NULL &&
proxy->var()->slot()->type() == Slot::LOOKUP) {
__ push(esi);
__ push(Immediate(proxy->name()));
__ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
__ push(eax);
} else {
// This expression cannot throw a reference error at the top level.
VisitForValue(expr->expression(), kStack);
}
__ CallRuntime(Runtime::kTypeof, 1); __ CallRuntime(Runtime::kTypeof, 1);
Apply(context_, eax); Apply(context_, eax);
break; break;
...@@ -2894,27 +2871,111 @@ void FullCodeGenerator::VisitCountOperation(CountOperation* expr) { ...@@ -2894,27 +2871,111 @@ void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
} }
void FullCodeGenerator::EmitNullCompare(bool strict, void FullCodeGenerator::VisitForTypeofValue(Expression* expr, Location where) {
Register obj, VariableProxy* proxy = expr->AsVariableProxy();
Register null_const, if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
Label* if_true, Comment cmnt(masm_, "Global variable");
Label* if_false, __ mov(eax, CodeGenerator::GlobalObject());
Register scratch) { __ mov(ecx, Immediate(proxy->name()));
__ cmp(obj, Operand(null_const)); Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
if (strict) { // Use a regular load, not a contextual load, to avoid a reference
Split(equal, if_true, if_false, NULL); // error.
__ call(ic, RelocInfo::CODE_TARGET);
if (where == kStack) __ push(eax);
} else if (proxy != NULL &&
proxy->var()->slot() != NULL &&
proxy->var()->slot()->type() == Slot::LOOKUP) {
__ push(esi);
__ push(Immediate(proxy->name()));
__ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
if (where == kStack) __ push(eax);
} else { } else {
// This expression cannot throw a reference error at the top level.
VisitForValue(expr, where);
}
}
bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
Expression* left,
Expression* right,
Label* if_true,
Label* if_false,
Label* fall_through) {
if (op != Token::EQ && op != Token::EQ_STRICT) return false;
// Check for the pattern: typeof <expression> == <string literal>.
Literal* right_literal = right->AsLiteral();
if (right_literal == NULL) return false;
Handle<Object> right_literal_value = right_literal->handle();
if (!right_literal_value->IsString()) return false;
UnaryOperation* left_unary = left->AsUnaryOperation();
if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
Handle<String> check = Handle<String>::cast(right_literal_value);
VisitForTypeofValue(left_unary->expression(), kAccumulator);
if (check->Equals(Heap::number_symbol())) {
__ test(eax, Immediate(kSmiTagMask));
__ j(zero, if_true);
__ cmp(FieldOperand(eax, HeapObject::kMapOffset),
Factory::heap_number_map());
Split(equal, if_true, if_false, fall_through);
} else if (check->Equals(Heap::string_symbol())) {
__ test(eax, Immediate(kSmiTagMask));
__ j(zero, if_false);
// Check for undetectable objects => false.
__ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
__ movzx_b(ecx, FieldOperand(edx, Map::kBitFieldOffset));
__ test(ecx, Immediate(1 << Map::kIsUndetectable));
__ j(not_zero, if_false);
__ CmpInstanceType(edx, FIRST_NONSTRING_TYPE);
Split(below, if_true, if_false, fall_through);
} else if (check->Equals(Heap::boolean_symbol())) {
__ cmp(eax, Factory::true_value());
__ j(equal, if_true); __ j(equal, if_true);
__ cmp(obj, Factory::undefined_value()); __ cmp(eax, Factory::false_value());
Split(equal, if_true, if_false, fall_through);
} else if (check->Equals(Heap::undefined_symbol())) {
__ cmp(eax, Factory::undefined_value());
__ j(equal, if_true); __ j(equal, if_true);
__ test(obj, Immediate(kSmiTagMask)); __ test(eax, Immediate(kSmiTagMask));
__ j(zero, if_false); __ j(zero, if_false);
// It can be an undetectable object. // Check for undetectable objects => true.
__ mov(scratch, FieldOperand(obj, HeapObject::kMapOffset)); __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
__ movzx_b(scratch, FieldOperand(scratch, Map::kBitFieldOffset)); __ movzx_b(ecx, FieldOperand(edx, Map::kBitFieldOffset));
__ test(scratch, Immediate(1 << Map::kIsUndetectable)); __ test(ecx, Immediate(1 << Map::kIsUndetectable));
Split(not_zero, if_true, if_false, NULL); Split(not_zero, if_true, if_false, fall_through);
} else if (check->Equals(Heap::function_symbol())) {
__ test(eax, Immediate(kSmiTagMask));
__ j(zero, if_false);
__ CmpObjectType(eax, JS_FUNCTION_TYPE, edx);
__ j(equal, if_true);
// Regular expressions => 'function' (they are callable).
__ CmpInstanceType(edx, JS_REGEXP_TYPE);
Split(equal, if_true, if_false, fall_through);
} else if (check->Equals(Heap::object_symbol())) {
__ test(eax, Immediate(kSmiTagMask));
__ j(zero, if_false);
__ cmp(eax, Factory::null_value());
__ j(equal, if_true);
// Regular expressions => 'function', not 'object'.
__ CmpObjectType(eax, JS_REGEXP_TYPE, edx);
__ j(equal, if_false);
// Check for undetectable objects => false.
__ movzx_b(ecx, FieldOperand(edx, Map::kBitFieldOffset));
__ test(ecx, Immediate(1 << Map::kIsUndetectable));
__ j(not_zero, if_false);
// Check for JS objects => true.
__ movzx_b(ecx, FieldOperand(edx, Map::kInstanceTypeOffset));
__ cmp(ecx, FIRST_JS_OBJECT_TYPE);
__ j(less, if_false);
__ cmp(ecx, LAST_JS_OBJECT_TYPE);
Split(less_equal, if_true, if_false, fall_through);
} else {
if (if_false != fall_through) __ jmp(if_false);
} }
return true;
} }
...@@ -2929,6 +2990,16 @@ void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) { ...@@ -2929,6 +2990,16 @@ void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
Label* if_false = NULL; Label* if_false = NULL;
PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false); PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
// First we try a fast inlined version of the compare when one of
// the operands is a literal.
Token::Value op = expr->op();
Expression* left = expr->left();
Expression* right = expr->right();
if (TryLiteralCompare(op, left, right, if_true, if_false, NULL)) {
Apply(context_, if_true, if_false);
return;
}
VisitForValue(expr->left(), kStack); VisitForValue(expr->left(), kStack);
switch (expr->op()) { switch (expr->op()) {
case Token::IN: case Token::IN:
...@@ -2956,24 +3027,10 @@ void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) { ...@@ -2956,24 +3027,10 @@ void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
case Token::EQ_STRICT: case Token::EQ_STRICT:
strict = true; strict = true;
// Fall through // Fall through
case Token::EQ: { case Token::EQ:
cc = equal; cc = equal;
__ pop(edx); __ pop(edx);
// If either operand is constant null we do a fast compare
// against null.
Literal* right_literal = expr->right()->AsLiteral();
Literal* left_literal = expr->left()->AsLiteral();
if (right_literal != NULL && right_literal->handle()->IsNull()) {
EmitNullCompare(strict, edx, eax, if_true, if_false, ecx);
Apply(context_, if_true, if_false);
return;
} else if (left_literal != NULL && left_literal->handle()->IsNull()) {
EmitNullCompare(strict, eax, edx, if_true, if_false, ecx);
Apply(context_, if_true, if_false);
return;
}
break; break;
}
case Token::LT: case Token::LT:
cc = less; cc = less;
__ pop(edx); __ pop(edx);
......
...@@ -2627,30 +2627,7 @@ void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) { ...@@ -2627,30 +2627,7 @@ void FullCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
case Token::TYPEOF: { case Token::TYPEOF: {
Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)"); Comment cmnt(masm_, "[ UnaryOperation (TYPEOF)");
VariableProxy* proxy = expr->expression()->AsVariableProxy(); VisitForTypeofValue(expr->expression(), kStack);
if (proxy != NULL &&
!proxy->var()->is_this() &&
proxy->var()->is_global()) {
Comment cmnt(masm_, "Global variable");
__ Move(rcx, proxy->name());
__ movq(rax, CodeGenerator::GlobalObject());
Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
// Use a regular load, not a contextual load, to avoid a reference
// error.
__ Call(ic, RelocInfo::CODE_TARGET);
__ push(rax);
} else if (proxy != NULL &&
proxy->var()->slot() != NULL &&
proxy->var()->slot()->type() == Slot::LOOKUP) {
__ push(rsi);
__ Push(proxy->name());
__ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
__ push(rax);
} else {
// This expression cannot throw a reference error at the top level.
VisitForValue(expr->expression(), kStack);
}
__ CallRuntime(Runtime::kTypeof, 1); __ CallRuntime(Runtime::kTypeof, 1);
Apply(context_, rax); Apply(context_, rax);
break; break;
...@@ -2888,26 +2865,110 @@ void FullCodeGenerator::VisitCountOperation(CountOperation* expr) { ...@@ -2888,26 +2865,110 @@ void FullCodeGenerator::VisitCountOperation(CountOperation* expr) {
} }
void FullCodeGenerator::EmitNullCompare(bool strict, void FullCodeGenerator::VisitForTypeofValue(Expression* expr, Location where) {
Register obj, VariableProxy* proxy = expr->AsVariableProxy();
Register null_const, if (proxy != NULL && !proxy->var()->is_this() && proxy->var()->is_global()) {
Label* if_true, Comment cmnt(masm_, "Global variable");
Label* if_false, __ Move(rcx, proxy->name());
Register scratch) { __ movq(rax, CodeGenerator::GlobalObject());
__ cmpq(obj, null_const); Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
if (strict) { // Use a regular load, not a contextual load, to avoid a reference
Split(equal, if_true, if_false, NULL); // error.
__ Call(ic, RelocInfo::CODE_TARGET);
if (where == kStack) __ push(rax);
} else if (proxy != NULL &&
proxy->var()->slot() != NULL &&
proxy->var()->slot()->type() == Slot::LOOKUP) {
__ push(rsi);
__ Push(proxy->name());
__ CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
if (where == kStack) __ push(rax);
} else { } else {
// This expression cannot throw a reference error at the top level.
VisitForValue(expr, where);
}
}
bool FullCodeGenerator::TryLiteralCompare(Token::Value op,
Expression* left,
Expression* right,
Label* if_true,
Label* if_false,
Label* fall_through) {
if (op != Token::EQ && op != Token::EQ_STRICT) return false;
// Check for the pattern: typeof <expression> == <string literal>.
Literal* right_literal = right->AsLiteral();
if (right_literal == NULL) return false;
Handle<Object> right_literal_value = right_literal->handle();
if (!right_literal_value->IsString()) return false;
UnaryOperation* left_unary = left->AsUnaryOperation();
if (left_unary == NULL || left_unary->op() != Token::TYPEOF) return false;
Handle<String> check = Handle<String>::cast(right_literal_value);
VisitForTypeofValue(left_unary->expression(), kAccumulator);
if (check->Equals(Heap::number_symbol())) {
Condition is_smi = masm_->CheckSmi(rax);
__ j(is_smi, if_true);
__ movq(rax, FieldOperand(rax, HeapObject::kMapOffset));
__ CompareRoot(rax, Heap::kHeapNumberMapRootIndex);
Split(equal, if_true, if_false, fall_through);
} else if (check->Equals(Heap::string_symbol())) {
Condition is_smi = masm_->CheckSmi(rax);
__ j(is_smi, if_false);
// Check for undetectable objects => false.
__ movq(rdx, FieldOperand(rax, HeapObject::kMapOffset));
__ testb(FieldOperand(rdx, Map::kBitFieldOffset),
Immediate(1 << Map::kIsUndetectable));
__ j(not_zero, if_false);
__ CmpInstanceType(rdx, FIRST_NONSTRING_TYPE);
Split(below, if_true, if_false, fall_through);
} else if (check->Equals(Heap::boolean_symbol())) {
__ CompareRoot(rax, Heap::kTrueValueRootIndex);
__ j(equal, if_true); __ j(equal, if_true);
__ CompareRoot(obj, Heap::kUndefinedValueRootIndex); __ CompareRoot(rax, Heap::kFalseValueRootIndex);
Split(equal, if_true, if_false, fall_through);
} else if (check->Equals(Heap::undefined_symbol())) {
__ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
__ j(equal, if_true); __ j(equal, if_true);
__ JumpIfSmi(obj, if_false); Condition is_smi = masm_->CheckSmi(rax);
// It can be an undetectable object. __ j(is_smi, if_false);
__ movq(scratch, FieldOperand(obj, HeapObject::kMapOffset)); // Check for undetectable objects => true.
__ testb(FieldOperand(scratch, Map::kBitFieldOffset), __ movq(rdx, FieldOperand(rax, HeapObject::kMapOffset));
__ testb(FieldOperand(rdx, Map::kBitFieldOffset),
Immediate(1 << Map::kIsUndetectable)); Immediate(1 << Map::kIsUndetectable));
Split(not_zero, if_true, if_false, NULL); Split(not_zero, if_true, if_false, fall_through);
} else if (check->Equals(Heap::function_symbol())) {
Condition is_smi = masm_->CheckSmi(rax);
__ j(is_smi, if_false);
__ CmpObjectType(rax, JS_FUNCTION_TYPE, rdx);
__ j(equal, if_true);
// Regular expressions => 'function' (they are callable).
__ CmpInstanceType(rdx, JS_REGEXP_TYPE);
Split(equal, if_true, if_false, fall_through);
} else if (check->Equals(Heap::object_symbol())) {
Condition is_smi = masm_->CheckSmi(rax);
__ j(is_smi, if_false);
__ CompareRoot(rax, Heap::kNullValueRootIndex);
__ j(equal, if_true);
// Regular expressions => 'function', not 'object'.
__ CmpObjectType(rax, JS_REGEXP_TYPE, rdx);
__ j(equal, if_false);
// Check for undetectable objects => false.
__ testb(FieldOperand(rdx, Map::kBitFieldOffset),
Immediate(1 << Map::kIsUndetectable));
__ j(not_zero, if_false);
// Check for JS objects => true.
__ CmpInstanceType(rdx, FIRST_JS_OBJECT_TYPE);
__ j(below, if_false);
__ CmpInstanceType(rdx, LAST_JS_OBJECT_TYPE);
Split(below_equal, if_true, if_false, fall_through);
} else {
if (if_false != fall_through) __ jmp(if_false);
} }
return true;
} }
...@@ -2921,6 +2982,16 @@ void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) { ...@@ -2921,6 +2982,16 @@ void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
Label* if_false = NULL; Label* if_false = NULL;
PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false); PrepareTest(&materialize_true, &materialize_false, &if_true, &if_false);
// First we try a fast inlined version of the compare when one of
// the operands is a literal.
Token::Value op = expr->op();
Expression* left = expr->left();
Expression* right = expr->right();
if (TryLiteralCompare(op, left, right, if_true, if_false, NULL)) {
Apply(context_, if_true, if_false);
return;
}
VisitForValue(expr->left(), kStack); VisitForValue(expr->left(), kStack);
switch (expr->op()) { switch (expr->op()) {
case Token::IN: case Token::IN:
...@@ -2948,24 +3019,10 @@ void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) { ...@@ -2948,24 +3019,10 @@ void FullCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
case Token::EQ_STRICT: case Token::EQ_STRICT:
strict = true; strict = true;
// Fall through. // Fall through.
case Token::EQ: { case Token::EQ:
cc = equal; cc = equal;
__ pop(rdx); __ pop(rdx);
// If either operand is constant null we do a fast compare
// against null.
Literal* right_literal = expr->right()->AsLiteral();
Literal* left_literal = expr->left()->AsLiteral();
if (right_literal != NULL && right_literal->handle()->IsNull()) {
EmitNullCompare(strict, rdx, rax, if_true, if_false, rcx);
Apply(context_, if_true, if_false);
return;
} else if (left_literal != NULL && left_literal->handle()->IsNull()) {
EmitNullCompare(strict, rax, rdx, if_true, if_false, rcx);
Apply(context_, if_true, if_false);
return;
}
break; break;
}
case Token::LT: case Token::LT:
cc = less; cc = less;
__ pop(rdx); __ pop(rdx);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment