| OLD | NEW |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 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 11 matching lines...) Expand all Loading... |
| 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 "v8.h" | 28 #include "v8.h" |
| 29 | 29 |
| 30 #include "codegen-inl.h" | 30 #include "codegen-inl.h" |
| 31 #include "fast-codegen.h" | 31 #include "fast-codegen.h" |
| 32 #include "data-flow.h" |
| 33 #include "scopes.h" |
| 32 | 34 |
| 33 namespace v8 { | 35 namespace v8 { |
| 34 namespace internal { | 36 namespace internal { |
| 35 | 37 |
| 38 #define BAILOUT(reason) \ |
| 39 do { \ |
| 40 if (FLAG_trace_bailout) { \ |
| 41 PrintF("%s\n", reason); \ |
| 42 } \ |
| 43 has_supported_syntax_ = false; \ |
| 44 return; \ |
| 45 } while (false) |
| 46 |
| 47 |
| 48 #define CHECK_BAILOUT \ |
| 49 do { \ |
| 50 if (!has_supported_syntax_) return; \ |
| 51 } while (false) |
| 52 |
| 53 |
| 54 void FastCodeGenSyntaxChecker::Check(CompilationInfo* info) { |
| 55 info_ = info; |
| 56 |
| 57 // We do not specialize if we do not have a receiver or if it is not a |
| 58 // JS object with fast mode properties. |
| 59 if (!info->has_receiver()) BAILOUT("No receiver"); |
| 60 if (!info->receiver()->IsJSObject()) BAILOUT("Receiver is not an object"); |
| 61 Handle<JSObject> object = Handle<JSObject>::cast(info->receiver()); |
| 62 if (!object->HasFastProperties()) BAILOUT("Receiver is in dictionary mode"); |
| 63 |
| 64 // We do not support stack or heap slots (both of which require |
| 65 // allocation). |
| 66 Scope* scope = info->scope(); |
| 67 if (scope->num_stack_slots() > 0) { |
| 68 BAILOUT("Function has stack-allocated locals"); |
| 69 } |
| 70 if (scope->num_heap_slots() > 0) { |
| 71 BAILOUT("Function has context-allocated locals"); |
| 72 } |
| 73 |
| 74 VisitDeclarations(scope->declarations()); |
| 75 CHECK_BAILOUT; |
| 76 |
| 77 // We do not support empty function bodies. |
| 78 if (info->function()->body()->is_empty()) { |
| 79 BAILOUT("Function has an empty body"); |
| 80 } |
| 81 VisitStatements(info->function()->body()); |
| 82 } |
| 83 |
| 84 |
| 85 void FastCodeGenSyntaxChecker::VisitDeclarations( |
| 86 ZoneList<Declaration*>* decls) { |
| 87 if (!decls->is_empty()) BAILOUT("Function has declarations"); |
| 88 } |
| 89 |
| 90 |
| 91 void FastCodeGenSyntaxChecker::VisitStatements(ZoneList<Statement*>* stmts) { |
| 92 if (stmts->length() != 1) { |
| 93 BAILOUT("Function body is not a singleton statement."); |
| 94 } |
| 95 Visit(stmts->at(0)); |
| 96 } |
| 97 |
| 98 |
| 99 void FastCodeGenSyntaxChecker::VisitDeclaration(Declaration* decl) { |
| 100 UNREACHABLE(); |
| 101 } |
| 102 |
| 103 |
| 104 void FastCodeGenSyntaxChecker::VisitBlock(Block* stmt) { |
| 105 VisitStatements(stmt->statements()); |
| 106 } |
| 107 |
| 108 |
| 109 void FastCodeGenSyntaxChecker::VisitExpressionStatement( |
| 110 ExpressionStatement* stmt) { |
| 111 Visit(stmt->expression()); |
| 112 } |
| 113 |
| 114 |
| 115 void FastCodeGenSyntaxChecker::VisitEmptyStatement(EmptyStatement* stmt) { |
| 116 // Supported. |
| 117 } |
| 118 |
| 119 |
| 120 void FastCodeGenSyntaxChecker::VisitIfStatement(IfStatement* stmt) { |
| 121 BAILOUT("IfStatement"); |
| 122 } |
| 123 |
| 124 |
| 125 void FastCodeGenSyntaxChecker::VisitContinueStatement(ContinueStatement* stmt) { |
| 126 BAILOUT("Continuestatement"); |
| 127 } |
| 128 |
| 129 |
| 130 void FastCodeGenSyntaxChecker::VisitBreakStatement(BreakStatement* stmt) { |
| 131 BAILOUT("BreakStatement"); |
| 132 } |
| 133 |
| 134 |
| 135 void FastCodeGenSyntaxChecker::VisitReturnStatement(ReturnStatement* stmt) { |
| 136 BAILOUT("ReturnStatement"); |
| 137 } |
| 138 |
| 139 |
| 140 void FastCodeGenSyntaxChecker::VisitWithEnterStatement( |
| 141 WithEnterStatement* stmt) { |
| 142 BAILOUT("WithEnterStatement"); |
| 143 } |
| 144 |
| 145 |
| 146 void FastCodeGenSyntaxChecker::VisitWithExitStatement(WithExitStatement* stmt) { |
| 147 BAILOUT("WithExitStatement"); |
| 148 } |
| 149 |
| 150 |
| 151 void FastCodeGenSyntaxChecker::VisitSwitchStatement(SwitchStatement* stmt) { |
| 152 BAILOUT("SwitchStatement"); |
| 153 } |
| 154 |
| 155 |
| 156 void FastCodeGenSyntaxChecker::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 157 BAILOUT("DoWhileStatement"); |
| 158 } |
| 159 |
| 160 |
| 161 void FastCodeGenSyntaxChecker::VisitWhileStatement(WhileStatement* stmt) { |
| 162 BAILOUT("WhileStatement"); |
| 163 } |
| 164 |
| 165 |
| 166 void FastCodeGenSyntaxChecker::VisitForStatement(ForStatement* stmt) { |
| 167 BAILOUT("ForStatement"); |
| 168 } |
| 169 |
| 170 |
| 171 void FastCodeGenSyntaxChecker::VisitForInStatement(ForInStatement* stmt) { |
| 172 BAILOUT("ForInStatement"); |
| 173 } |
| 174 |
| 175 |
| 176 void FastCodeGenSyntaxChecker::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 177 BAILOUT("TryCatchStatement"); |
| 178 } |
| 179 |
| 180 |
| 181 void FastCodeGenSyntaxChecker::VisitTryFinallyStatement( |
| 182 TryFinallyStatement* stmt) { |
| 183 BAILOUT("TryFinallyStatement"); |
| 184 } |
| 185 |
| 186 |
| 187 void FastCodeGenSyntaxChecker::VisitDebuggerStatement( |
| 188 DebuggerStatement* stmt) { |
| 189 BAILOUT("DebuggerStatement"); |
| 190 } |
| 191 |
| 192 |
| 193 void FastCodeGenSyntaxChecker::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 194 BAILOUT("FunctionLiteral"); |
| 195 } |
| 196 |
| 197 |
| 198 void FastCodeGenSyntaxChecker::VisitFunctionBoilerplateLiteral( |
| 199 FunctionBoilerplateLiteral* expr) { |
| 200 BAILOUT("FunctionBoilerplateLiteral"); |
| 201 } |
| 202 |
| 203 |
| 204 void FastCodeGenSyntaxChecker::VisitConditional(Conditional* expr) { |
| 205 BAILOUT("Conditional"); |
| 206 } |
| 207 |
| 208 |
| 209 void FastCodeGenSyntaxChecker::VisitSlot(Slot* expr) { |
| 210 UNREACHABLE(); |
| 211 } |
| 212 |
| 213 |
| 214 void FastCodeGenSyntaxChecker::VisitVariableProxy(VariableProxy* expr) { |
| 215 // Only global variable references are supported. |
| 216 Variable* var = expr->var(); |
| 217 if (!var->is_global() || var->is_this()) BAILOUT("Non-global variable"); |
| 218 |
| 219 // Check if the global variable is existing and non-deletable. |
| 220 if (info()->has_global_object()) { |
| 221 LookupResult lookup; |
| 222 info()->global_object()->Lookup(*expr->name(), &lookup); |
| 223 if (!lookup.IsProperty()) { |
| 224 BAILOUT("Non-existing global variable"); |
| 225 } |
| 226 // We do not handle global variables with accessors or interceptors. |
| 227 if (lookup.type() != NORMAL) { |
| 228 BAILOUT("Global variable with accessors or interceptors."); |
| 229 } |
| 230 // We do not handle deletable global variables. |
| 231 if (!lookup.IsDontDelete()) { |
| 232 BAILOUT("Deletable global variable"); |
| 233 } |
| 234 } |
| 235 } |
| 236 |
| 237 |
| 238 void FastCodeGenSyntaxChecker::VisitLiteral(Literal* expr) { |
| 239 BAILOUT("Literal"); |
| 240 } |
| 241 |
| 242 |
| 243 void FastCodeGenSyntaxChecker::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 244 BAILOUT("RegExpLiteral"); |
| 245 } |
| 246 |
| 247 |
| 248 void FastCodeGenSyntaxChecker::VisitObjectLiteral(ObjectLiteral* expr) { |
| 249 BAILOUT("ObjectLiteral"); |
| 250 } |
| 251 |
| 252 |
| 253 void FastCodeGenSyntaxChecker::VisitArrayLiteral(ArrayLiteral* expr) { |
| 254 BAILOUT("ArrayLiteral"); |
| 255 } |
| 256 |
| 257 |
| 258 void FastCodeGenSyntaxChecker::VisitCatchExtensionObject( |
| 259 CatchExtensionObject* expr) { |
| 260 BAILOUT("CatchExtensionObject"); |
| 261 } |
| 262 |
| 263 |
| 264 void FastCodeGenSyntaxChecker::VisitAssignment(Assignment* expr) { |
| 265 // Simple assignments to (named) this properties are supported. |
| 266 if (expr->op() != Token::ASSIGN) BAILOUT("Non-simple assignment"); |
| 267 |
| 268 Property* prop = expr->target()->AsProperty(); |
| 269 if (prop == NULL) BAILOUT("Non-property assignment"); |
| 270 VariableProxy* proxy = prop->obj()->AsVariableProxy(); |
| 271 if (proxy == NULL || !proxy->var()->is_this()) { |
| 272 BAILOUT("Non-this-property assignment"); |
| 273 } |
| 274 if (!prop->key()->IsPropertyName()) { |
| 275 BAILOUT("Non-named-property assignment"); |
| 276 } |
| 277 |
| 278 // We will only specialize for fields on the object itself. |
| 279 // Expression::IsPropertyName implies that the name is a literal |
| 280 // symbol but we do not assume that. |
| 281 Literal* key = prop->key()->AsLiteral(); |
| 282 if (key != NULL && key->handle()->IsString()) { |
| 283 Handle<Object> receiver = info()->receiver(); |
| 284 Handle<String> name = Handle<String>::cast(key->handle()); |
| 285 LookupResult lookup; |
| 286 receiver->Lookup(*name, &lookup); |
| 287 if (!lookup.IsProperty()) { |
| 288 BAILOUT("Assigned property not found at compile time"); |
| 289 } |
| 290 if (lookup.holder() != *receiver) BAILOUT("Non-own property assignment"); |
| 291 if (!lookup.type() == FIELD) BAILOUT("Non-field property assignment"); |
| 292 } else { |
| 293 UNREACHABLE(); |
| 294 BAILOUT("Unexpected non-string-literal property key"); |
| 295 } |
| 296 |
| 297 Visit(expr->value()); |
| 298 } |
| 299 |
| 300 |
| 301 void FastCodeGenSyntaxChecker::VisitThrow(Throw* expr) { |
| 302 BAILOUT("Throw"); |
| 303 } |
| 304 |
| 305 |
| 306 void FastCodeGenSyntaxChecker::VisitProperty(Property* expr) { |
| 307 // We support named this property references. |
| 308 VariableProxy* proxy = expr->obj()->AsVariableProxy(); |
| 309 if (proxy == NULL || !proxy->var()->is_this()) { |
| 310 BAILOUT("Non-this-property reference"); |
| 311 } |
| 312 if (!expr->key()->IsPropertyName()) { |
| 313 BAILOUT("Non-named-property reference"); |
| 314 } |
| 315 |
| 316 // We will only specialize for fields on the object itself. |
| 317 // Expression::IsPropertyName implies that the name is a literal |
| 318 // symbol but we do not assume that. |
| 319 Literal* key = expr->key()->AsLiteral(); |
| 320 if (key != NULL && key->handle()->IsString()) { |
| 321 Handle<Object> receiver = info()->receiver(); |
| 322 Handle<String> name = Handle<String>::cast(key->handle()); |
| 323 LookupResult lookup; |
| 324 receiver->Lookup(*name, &lookup); |
| 325 if (!lookup.IsProperty()) { |
| 326 BAILOUT("Referenced property not found at compile time"); |
| 327 } |
| 328 if (lookup.holder() != *receiver) BAILOUT("Non-own property reference"); |
| 329 if (!lookup.type() == FIELD) BAILOUT("Non-field property reference"); |
| 330 } else { |
| 331 UNREACHABLE(); |
| 332 BAILOUT("Unexpected non-string-literal property key"); |
| 333 } |
| 334 } |
| 335 |
| 336 |
| 337 void FastCodeGenSyntaxChecker::VisitCall(Call* expr) { |
| 338 BAILOUT("Call"); |
| 339 } |
| 340 |
| 341 |
| 342 void FastCodeGenSyntaxChecker::VisitCallNew(CallNew* expr) { |
| 343 BAILOUT("CallNew"); |
| 344 } |
| 345 |
| 346 |
| 347 void FastCodeGenSyntaxChecker::VisitCallRuntime(CallRuntime* expr) { |
| 348 BAILOUT("CallRuntime"); |
| 349 } |
| 350 |
| 351 |
| 352 void FastCodeGenSyntaxChecker::VisitUnaryOperation(UnaryOperation* expr) { |
| 353 BAILOUT("UnaryOperation"); |
| 354 } |
| 355 |
| 356 |
| 357 void FastCodeGenSyntaxChecker::VisitCountOperation(CountOperation* expr) { |
| 358 BAILOUT("CountOperation"); |
| 359 } |
| 360 |
| 361 |
| 362 void FastCodeGenSyntaxChecker::VisitBinaryOperation(BinaryOperation* expr) { |
| 363 // We support bitwise OR. |
| 364 switch (expr->op()) { |
| 365 case Token::COMMA: |
| 366 BAILOUT("BinaryOperation COMMA"); |
| 367 case Token::OR: |
| 368 BAILOUT("BinaryOperation OR"); |
| 369 case Token::AND: |
| 370 BAILOUT("BinaryOperation AND"); |
| 371 |
| 372 case Token::BIT_OR: |
| 373 // We support expressions nested on the left because they only require |
| 374 // a pair of registers to keep all intermediate values in registers |
| 375 // (i.e., the expression stack has height no more than two). |
| 376 if (!expr->right()->IsLeaf()) BAILOUT("expression nested on right"); |
| 377 |
| 378 // We do not allow subexpressions with side effects because we |
| 379 // (currently) bail out to the beginning of the full function. The |
| 380 // only expressions with side effects that we would otherwise handle |
| 381 // are assignments. |
| 382 if (expr->left()->AsAssignment() != NULL || |
| 383 expr->right()->AsAssignment() != NULL) { |
| 384 BAILOUT("subexpression of binary operation has side effects"); |
| 385 } |
| 386 |
| 387 Visit(expr->left()); |
| 388 CHECK_BAILOUT; |
| 389 Visit(expr->right()); |
| 390 break; |
| 391 |
| 392 case Token::BIT_XOR: |
| 393 BAILOUT("BinaryOperation BIT_XOR"); |
| 394 case Token::BIT_AND: |
| 395 BAILOUT("BinaryOperation BIT_AND"); |
| 396 case Token::SHL: |
| 397 BAILOUT("BinaryOperation SHL"); |
| 398 case Token::SAR: |
| 399 BAILOUT("BinaryOperation SAR"); |
| 400 case Token::SHR: |
| 401 BAILOUT("BinaryOperation SHR"); |
| 402 case Token::ADD: |
| 403 BAILOUT("BinaryOperation ADD"); |
| 404 case Token::SUB: |
| 405 BAILOUT("BinaryOperation SUB"); |
| 406 case Token::MUL: |
| 407 BAILOUT("BinaryOperation MUL"); |
| 408 case Token::DIV: |
| 409 BAILOUT("BinaryOperation DIV"); |
| 410 case Token::MOD: |
| 411 BAILOUT("BinaryOperation MOD"); |
| 412 default: |
| 413 UNREACHABLE(); |
| 414 } |
| 415 } |
| 416 |
| 417 |
| 418 void FastCodeGenSyntaxChecker::VisitCompareOperation(CompareOperation* expr) { |
| 419 BAILOUT("CompareOperation"); |
| 420 } |
| 421 |
| 422 |
| 423 void FastCodeGenSyntaxChecker::VisitThisFunction(ThisFunction* expr) { |
| 424 BAILOUT("ThisFunction"); |
| 425 } |
| 426 |
| 427 #undef BAILOUT |
| 428 #undef CHECK_BAILOUT |
| 429 |
| 430 |
| 36 #define __ ACCESS_MASM(masm()) | 431 #define __ ACCESS_MASM(masm()) |
| 37 | 432 |
| 38 void FastCodeGenerator::EmitLoadReceiver(Register reg) { | 433 Handle<Code> FastCodeGenerator::MakeCode(CompilationInfo* info) { |
| 434 // Label the AST before calling MakeCodePrologue, so AST node numbers are |
| 435 // printed with the AST. |
| 436 AstLabeler labeler; |
| 437 labeler.Label(info); |
| 438 |
| 439 LivenessAnalyzer analyzer; |
| 440 analyzer.Analyze(info->function()); |
| 441 |
| 442 CodeGenerator::MakeCodePrologue(info); |
| 443 |
| 444 const int kInitialBufferSize = 4 * KB; |
| 445 MacroAssembler masm(NULL, kInitialBufferSize); |
| 446 |
| 447 // Generate the fast-path code. |
| 448 FastCodeGenerator fast_cgen(&masm); |
| 449 fast_cgen.Generate(info); |
| 450 if (fast_cgen.HasStackOverflow()) { |
| 451 ASSERT(!Top::has_pending_exception()); |
| 452 return Handle<Code>::null(); |
| 453 } |
| 454 |
| 455 // Generate the full code for the function in bailout mode, using the same |
| 456 // macro assembler. |
| 457 CodeGenerator cgen(&masm); |
| 458 CodeGeneratorScope scope(&cgen); |
| 459 info->set_mode(CompilationInfo::SECONDARY); |
| 460 cgen.Generate(info); |
| 461 if (cgen.HasStackOverflow()) { |
| 462 ASSERT(!Top::has_pending_exception()); |
| 463 return Handle<Code>::null(); |
| 464 } |
| 465 |
| 466 Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP); |
| 467 return CodeGenerator::MakeCodeEpilogue(&masm, flags, info); |
| 468 } |
| 469 |
| 470 |
| 471 Register FastCodeGenerator::accumulator0() { return eax; } |
| 472 Register FastCodeGenerator::accumulator1() { return edx; } |
| 473 Register FastCodeGenerator::scratch0() { return ecx; } |
| 474 Register FastCodeGenerator::scratch1() { return edi; } |
| 475 Register FastCodeGenerator::receiver_reg() { return ebx; } |
| 476 Register FastCodeGenerator::context_reg() { return esi; } |
| 477 |
| 478 |
| 479 void FastCodeGenerator::EmitLoadReceiver() { |
| 39 // Offset 2 is due to return address and saved frame pointer. | 480 // Offset 2 is due to return address and saved frame pointer. |
| 40 int index = 2 + function()->scope()->num_parameters(); | 481 int index = 2 + function()->scope()->num_parameters(); |
| 41 __ mov(reg, Operand(ebp, index * kPointerSize)); | 482 __ mov(receiver_reg(), Operand(ebp, index * kPointerSize)); |
| 42 } | |
| 43 | |
| 44 | |
| 45 void FastCodeGenerator::EmitReceiverMapCheck() { | |
| 46 Comment cmnt(masm(), ";; MapCheck(this)"); | |
| 47 if (FLAG_print_ir) { | |
| 48 PrintF("MapCheck(this)\n"); | |
| 49 } | |
| 50 | |
| 51 ASSERT(info()->has_receiver() && info()->receiver()->IsHeapObject()); | |
| 52 Handle<HeapObject> object = Handle<HeapObject>::cast(info()->receiver()); | |
| 53 Handle<Map> map(object->map()); | |
| 54 | |
| 55 EmitLoadReceiver(edx); | |
| 56 __ CheckMap(edx, map, bailout(), false); | |
| 57 } | |
| 58 | |
| 59 | |
| 60 void FastCodeGenerator::EmitGlobalMapCheck() { | |
| 61 Comment cmnt(masm(), ";; GlobalMapCheck"); | |
| 62 if (FLAG_print_ir) { | |
| 63 PrintF(";; GlobalMapCheck()"); | |
| 64 } | |
| 65 | |
| 66 ASSERT(info()->has_global_object()); | |
| 67 Handle<Map> map(info()->global_object()->map()); | |
| 68 | |
| 69 __ mov(ebx, CodeGenerator::GlobalObject()); | |
| 70 __ CheckMap(ebx, map, bailout(), true); | |
| 71 } | 483 } |
| 72 | 484 |
| 73 | 485 |
| 74 void FastCodeGenerator::EmitGlobalVariableLoad(Handle<Object> cell) { | 486 void FastCodeGenerator::EmitGlobalVariableLoad(Handle<Object> cell) { |
| 487 ASSERT(!destination().is(no_reg)); |
| 75 ASSERT(cell->IsJSGlobalPropertyCell()); | 488 ASSERT(cell->IsJSGlobalPropertyCell()); |
| 76 __ mov(eax, Immediate(cell)); | 489 |
| 77 __ mov(eax, FieldOperand(eax, JSGlobalPropertyCell::kValueOffset)); | 490 __ mov(destination(), Immediate(cell)); |
| 491 __ mov(destination(), |
| 492 FieldOperand(destination(), JSGlobalPropertyCell::kValueOffset)); |
| 78 if (FLAG_debug_code) { | 493 if (FLAG_debug_code) { |
| 79 __ cmp(eax, Factory::the_hole_value()); | 494 __ cmp(destination(), Factory::the_hole_value()); |
| 80 __ Check(not_equal, "DontDelete cells can't contain the hole"); | 495 __ Check(not_equal, "DontDelete cells can't contain the hole"); |
| 81 } | 496 } |
| 82 } | 497 |
| 83 | 498 // The loaded value is not known to be a smi. |
| 84 | 499 clear_as_smi(destination()); |
| 500 } |
| 501 |
| 502 |
| 85 void FastCodeGenerator::EmitThisPropertyStore(Handle<String> name) { | 503 void FastCodeGenerator::EmitThisPropertyStore(Handle<String> name) { |
| 86 LookupResult lookup; | 504 LookupResult lookup; |
| 87 info()->receiver()->Lookup(*name, &lookup); | 505 info()->receiver()->Lookup(*name, &lookup); |
| 88 | 506 |
| 89 ASSERT(lookup.holder() == *info()->receiver()); | 507 ASSERT(lookup.holder() == *info()->receiver()); |
| 90 ASSERT(lookup.type() == FIELD); | 508 ASSERT(lookup.type() == FIELD); |
| 91 Handle<Map> map(Handle<HeapObject>::cast(info()->receiver())->map()); | 509 Handle<Map> map(Handle<HeapObject>::cast(info()->receiver())->map()); |
| 92 int index = lookup.GetFieldIndex() - map->inobject_properties(); | 510 int index = lookup.GetFieldIndex() - map->inobject_properties(); |
| 93 int offset = index * kPointerSize; | 511 int offset = index * kPointerSize; |
| 94 | 512 |
| 95 // Negative offsets are inobject properties. | 513 // We will emit the write barrier unless the stored value is statically |
| 514 // known to be a smi. |
| 515 bool needs_write_barrier = !is_smi(accumulator0()); |
| 516 |
| 517 // Perform the store. Negative offsets are inobject properties. |
| 96 if (offset < 0) { | 518 if (offset < 0) { |
| 97 offset += map->instance_size(); | 519 offset += map->instance_size(); |
| 98 __ mov(ecx, edx); // Copy receiver for write barrier. | 520 __ mov(FieldOperand(receiver_reg(), offset), accumulator0()); |
| 521 if (needs_write_barrier) { |
| 522 // Preserve receiver from write barrier. |
| 523 __ mov(scratch0(), receiver_reg()); |
| 524 } |
| 99 } else { | 525 } else { |
| 100 offset += FixedArray::kHeaderSize; | 526 offset += FixedArray::kHeaderSize; |
| 101 __ mov(ecx, FieldOperand(edx, JSObject::kPropertiesOffset)); | 527 __ mov(scratch0(), |
| 528 FieldOperand(receiver_reg(), JSObject::kPropertiesOffset)); |
| 529 __ mov(FieldOperand(scratch0(), offset), accumulator0()); |
| 102 } | 530 } |
| 103 // Perform the store. | 531 |
| 104 __ mov(FieldOperand(ecx, offset), eax); | 532 if (needs_write_barrier) { |
| 105 // Preserve value from write barrier in case it's needed. | 533 if (destination().is(no_reg)) { |
| 106 __ mov(ebx, eax); | 534 // After RecordWrite accumulator0 is only accidently a smi, but it is |
| 107 __ RecordWrite(ecx, offset, ebx, edi); | 535 // already marked as not known to be one. |
| 536 __ RecordWrite(scratch0(), offset, accumulator0(), scratch1()); |
| 537 } else { |
| 538 // Copy the value to the other accumulator to preserve a copy from the |
| 539 // write barrier. One of the accumulators is available as a scratch |
| 540 // register. Neither is a smi. |
| 541 __ mov(accumulator1(), accumulator0()); |
| 542 clear_as_smi(accumulator1()); |
| 543 Register value_scratch = other_accumulator(destination()); |
| 544 __ RecordWrite(scratch0(), offset, value_scratch, scratch1()); |
| 545 } |
| 546 } else if (destination().is(accumulator1())) { |
| 547 __ mov(accumulator1(), accumulator0()); |
| 548 // Is a smi because we do not need the write barrier. |
| 549 set_as_smi(accumulator1()); |
| 550 } |
| 551 } |
| 552 |
| 553 |
| 554 void FastCodeGenerator::EmitThisPropertyLoad(Handle<String> name) { |
| 555 ASSERT(!destination().is(no_reg)); |
| 556 LookupResult lookup; |
| 557 info()->receiver()->Lookup(*name, &lookup); |
| 558 |
| 559 ASSERT(lookup.holder() == *info()->receiver()); |
| 560 ASSERT(lookup.type() == FIELD); |
| 561 Handle<Map> map(Handle<HeapObject>::cast(info()->receiver())->map()); |
| 562 int index = lookup.GetFieldIndex() - map->inobject_properties(); |
| 563 int offset = index * kPointerSize; |
| 564 |
| 565 // Perform the load. Negative offsets are inobject properties. |
| 566 if (offset < 0) { |
| 567 offset += map->instance_size(); |
| 568 __ mov(destination(), FieldOperand(receiver_reg(), offset)); |
| 569 } else { |
| 570 offset += FixedArray::kHeaderSize; |
| 571 __ mov(scratch0(), |
| 572 FieldOperand(receiver_reg(), JSObject::kPropertiesOffset)); |
| 573 __ mov(destination(), FieldOperand(scratch0(), offset)); |
| 574 } |
| 575 |
| 576 // The loaded value is not known to be a smi. |
| 577 clear_as_smi(destination()); |
| 578 } |
| 579 |
| 580 |
| 581 void FastCodeGenerator::EmitBitOr() { |
| 582 if (is_smi(accumulator0()) && is_smi(accumulator1())) { |
| 583 // If both operands are known to be a smi then there is no need to check |
| 584 // the operands or result. There is no need to perform the operation in |
| 585 // an effect context. |
| 586 if (!destination().is(no_reg)) { |
| 587 // Leave the result in the destination register. Bitwise or is |
| 588 // commutative. |
| 589 __ or_(destination(), Operand(other_accumulator(destination()))); |
| 590 } |
| 591 } else { |
| 592 // Left is in accumulator1, right in accumulator0. |
| 593 Label* bailout = NULL; |
| 594 if (destination().is(accumulator0())) { |
| 595 __ mov(scratch0(), accumulator0()); |
| 596 __ or_(destination(), Operand(accumulator1())); // Or is commutative. |
| 597 __ test(destination(), Immediate(kSmiTagMask)); |
| 598 bailout = info()->AddBailout(accumulator1(), scratch0()); // Left, right. |
| 599 } else if (destination().is(accumulator1())) { |
| 600 __ mov(scratch0(), accumulator1()); |
| 601 __ or_(destination(), Operand(accumulator0())); |
| 602 __ test(destination(), Immediate(kSmiTagMask)); |
| 603 bailout = info()->AddBailout(scratch0(), accumulator0()); |
| 604 } else { |
| 605 ASSERT(destination().is(no_reg)); |
| 606 __ mov(scratch0(), accumulator1()); |
| 607 __ or_(scratch0(), Operand(accumulator0())); |
| 608 __ test(scratch0(), Immediate(kSmiTagMask)); |
| 609 bailout = info()->AddBailout(accumulator1(), accumulator0()); |
| 610 } |
| 611 __ j(not_zero, bailout, not_taken); |
| 612 } |
| 613 |
| 614 // If we didn't bailout, the result (in fact, both inputs too) is known to |
| 615 // be a smi. |
| 616 set_as_smi(accumulator0()); |
| 617 set_as_smi(accumulator1()); |
| 108 } | 618 } |
| 109 | 619 |
| 110 | 620 |
| 111 void FastCodeGenerator::Generate(CompilationInfo* compilation_info) { | 621 void FastCodeGenerator::Generate(CompilationInfo* compilation_info) { |
| 112 ASSERT(info_ == NULL); | 622 ASSERT(info_ == NULL); |
| 113 info_ = compilation_info; | 623 info_ = compilation_info; |
| 114 | 624 |
| 115 // Save the caller's frame pointer and set up our own. | 625 // Save the caller's frame pointer and set up our own. |
| 116 Comment prologue_cmnt(masm(), ";; Prologue"); | 626 Comment prologue_cmnt(masm(), ";; Prologue"); |
| 117 __ push(ebp); | 627 __ push(ebp); |
| 118 __ mov(ebp, esp); | 628 __ mov(ebp, esp); |
| 119 __ push(esi); // Context. | 629 __ push(esi); // Context. |
| 120 __ push(edi); // Closure. | 630 __ push(edi); // Closure. |
| 121 // Note that we keep a live register reference to esi (context) at this | 631 // Note that we keep a live register reference to esi (context) at this |
| 122 // point. | 632 // point. |
| 123 | 633 |
| 124 // Receiver (this) is allocated to edx if there are this properties. | 634 Label* bailout_to_beginning = info()->AddBailout(); |
| 125 if (info()->has_this_properties()) EmitReceiverMapCheck(); | 635 // Receiver (this) is allocated to a fixed register. |
| 126 | 636 if (info()->has_this_properties()) { |
| 127 // If there is a global variable access check if the global object | 637 Comment cmnt(masm(), ";; MapCheck(this)"); |
| 128 // is the same as at lazy-compilation time. | 638 if (FLAG_print_ir) { |
| 129 if (info()->has_globals()) EmitGlobalMapCheck(); | 639 PrintF("#: MapCheck(this)\n"); |
| 640 } |
| 641 ASSERT(info()->has_receiver() && info()->receiver()->IsHeapObject()); |
| 642 Handle<HeapObject> object = Handle<HeapObject>::cast(info()->receiver()); |
| 643 Handle<Map> map(object->map()); |
| 644 EmitLoadReceiver(); |
| 645 __ CheckMap(receiver_reg(), map, bailout_to_beginning, false); |
| 646 } |
| 647 |
| 648 // If there is a global variable access check if the global object is the |
| 649 // same as at lazy-compilation time. |
| 650 if (info()->has_globals()) { |
| 651 Comment cmnt(masm(), ";; MapCheck(GLOBAL)"); |
| 652 if (FLAG_print_ir) { |
| 653 PrintF("#: MapCheck(GLOBAL)\n"); |
| 654 } |
| 655 ASSERT(info()->has_global_object()); |
| 656 Handle<Map> map(info()->global_object()->map()); |
| 657 __ mov(scratch0(), CodeGenerator::GlobalObject()); |
| 658 __ CheckMap(scratch0(), map, bailout_to_beginning, true); |
| 659 } |
| 130 | 660 |
| 131 VisitStatements(function()->body()); | 661 VisitStatements(function()->body()); |
| 132 | 662 |
| 133 Comment return_cmnt(masm(), ";; Return(<undefined>)"); | 663 Comment return_cmnt(masm(), ";; Return(<undefined>)"); |
| 664 if (FLAG_print_ir) { |
| 665 PrintF("#: Return(<undefined>)\n"); |
| 666 } |
| 134 __ mov(eax, Factory::undefined_value()); | 667 __ mov(eax, Factory::undefined_value()); |
| 135 | |
| 136 Comment epilogue_cmnt(masm(), ";; Epilogue"); | |
| 137 __ mov(esp, ebp); | 668 __ mov(esp, ebp); |
| 138 __ pop(ebp); | 669 __ pop(ebp); |
| 139 __ ret((scope()->num_parameters() + 1) * kPointerSize); | 670 __ ret((scope()->num_parameters() + 1) * kPointerSize); |
| 140 | 671 } |
| 141 __ bind(&bailout_); | 672 |
| 142 } | 673 |
| 143 | 674 void FastCodeGenerator::VisitDeclaration(Declaration* decl) { |
| 675 UNREACHABLE(); |
| 676 } |
| 677 |
| 678 |
| 679 void FastCodeGenerator::VisitBlock(Block* stmt) { |
| 680 VisitStatements(stmt->statements()); |
| 681 } |
| 682 |
| 683 |
| 684 void FastCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) { |
| 685 Visit(stmt->expression()); |
| 686 } |
| 687 |
| 688 |
| 689 void FastCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) { |
| 690 // Nothing to do. |
| 691 } |
| 692 |
| 693 |
| 694 void FastCodeGenerator::VisitIfStatement(IfStatement* stmt) { |
| 695 UNREACHABLE(); |
| 696 } |
| 697 |
| 698 |
| 699 void FastCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) { |
| 700 UNREACHABLE(); |
| 701 } |
| 702 |
| 703 |
| 704 void FastCodeGenerator::VisitBreakStatement(BreakStatement* stmt) { |
| 705 UNREACHABLE(); |
| 706 } |
| 707 |
| 708 |
| 709 void FastCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) { |
| 710 UNREACHABLE(); |
| 711 } |
| 712 |
| 713 |
| 714 void FastCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) { |
| 715 UNREACHABLE(); |
| 716 } |
| 717 |
| 718 |
| 719 void FastCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) { |
| 720 UNREACHABLE(); |
| 721 } |
| 722 |
| 723 |
| 724 void FastCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) { |
| 725 UNREACHABLE(); |
| 726 } |
| 727 |
| 728 |
| 729 void FastCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 730 UNREACHABLE(); |
| 731 } |
| 732 |
| 733 |
| 734 void FastCodeGenerator::VisitWhileStatement(WhileStatement* stmt) { |
| 735 UNREACHABLE(); |
| 736 } |
| 737 |
| 738 |
| 739 void FastCodeGenerator::VisitForStatement(ForStatement* stmt) { |
| 740 UNREACHABLE(); |
| 741 } |
| 742 |
| 743 |
| 744 void FastCodeGenerator::VisitForInStatement(ForInStatement* stmt) { |
| 745 UNREACHABLE(); |
| 746 } |
| 747 |
| 748 |
| 749 void FastCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 750 UNREACHABLE(); |
| 751 } |
| 752 |
| 753 |
| 754 void FastCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
| 755 UNREACHABLE(); |
| 756 } |
| 757 |
| 758 |
| 759 void FastCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) { |
| 760 UNREACHABLE(); |
| 761 } |
| 762 |
| 763 |
| 764 void FastCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 765 UNREACHABLE(); |
| 766 } |
| 767 |
| 768 |
| 769 void FastCodeGenerator::VisitFunctionBoilerplateLiteral( |
| 770 FunctionBoilerplateLiteral* expr) { |
| 771 UNREACHABLE(); |
| 772 } |
| 773 |
| 774 |
| 775 void FastCodeGenerator::VisitConditional(Conditional* expr) { |
| 776 UNREACHABLE(); |
| 777 } |
| 778 |
| 779 |
| 780 void FastCodeGenerator::VisitSlot(Slot* expr) { |
| 781 UNREACHABLE(); |
| 782 } |
| 783 |
| 784 |
| 785 void FastCodeGenerator::VisitVariableProxy(VariableProxy* expr) { |
| 786 ASSERT(expr->var()->is_global() && !expr->var()->is_this()); |
| 787 // Check if we can compile a global variable load directly from the cell. |
| 788 ASSERT(info()->has_global_object()); |
| 789 LookupResult lookup; |
| 790 info()->global_object()->Lookup(*expr->name(), &lookup); |
| 791 // We only support normal (non-accessor/interceptor) DontDelete properties |
| 792 // for now. |
| 793 ASSERT(lookup.IsProperty()); |
| 794 ASSERT_EQ(NORMAL, lookup.type()); |
| 795 ASSERT(lookup.IsDontDelete()); |
| 796 Handle<Object> cell(info()->global_object()->GetPropertyCell(&lookup)); |
| 797 |
| 798 // Global variable lookups do not have side effects, so we do not need to |
| 799 // emit code if we are in an effect context. |
| 800 if (!destination().is(no_reg)) { |
| 801 Comment cmnt(masm(), ";; Global"); |
| 802 if (FLAG_print_ir) { |
| 803 SmartPointer<char> name = expr->name()->ToCString(); |
| 804 PrintF("%d: t%d = Global(%s) // last_use = %d\n", expr->num(), |
| 805 expr->num(), *name, expr->var_def()->last_use()->num()); |
| 806 } |
| 807 EmitGlobalVariableLoad(cell); |
| 808 } |
| 809 } |
| 810 |
| 811 |
| 812 void FastCodeGenerator::VisitLiteral(Literal* expr) { |
| 813 UNREACHABLE(); |
| 814 } |
| 815 |
| 816 |
| 817 void FastCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 818 UNREACHABLE(); |
| 819 } |
| 820 |
| 821 |
| 822 void FastCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) { |
| 823 UNREACHABLE(); |
| 824 } |
| 825 |
| 826 |
| 827 void FastCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) { |
| 828 UNREACHABLE(); |
| 829 } |
| 830 |
| 831 |
| 832 void FastCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) { |
| 833 UNREACHABLE(); |
| 834 } |
| 835 |
| 836 |
| 837 void FastCodeGenerator::VisitAssignment(Assignment* expr) { |
| 838 // Known to be a simple this property assignment. Effectively a unary |
| 839 // operation. |
| 840 { Register my_destination = destination(); |
| 841 set_destination(accumulator0()); |
| 842 Visit(expr->value()); |
| 843 set_destination(my_destination); |
| 844 } |
| 845 |
| 846 Property* prop = expr->target()->AsProperty(); |
| 847 ASSERT_NOT_NULL(prop); |
| 848 ASSERT_NOT_NULL(prop->obj()->AsVariableProxy()); |
| 849 ASSERT(prop->obj()->AsVariableProxy()->var()->is_this()); |
| 850 ASSERT(prop->key()->IsPropertyName()); |
| 851 Handle<String> name = |
| 852 Handle<String>::cast(prop->key()->AsLiteral()->handle()); |
| 853 |
| 854 Comment cmnt(masm(), ";; Store to this"); |
| 855 if (FLAG_print_ir) { |
| 856 SmartPointer<char> name_string = name->ToCString(); |
| 857 PrintF("%d: ", expr->num()); |
| 858 if (!destination().is(no_reg)) PrintF("t%d = ", expr->num()); |
| 859 PrintF("Store(this, \"%s\", t%d) // last_use(this) = %d\n", *name_string, |
| 860 expr->value()->num(), |
| 861 expr->var_def()->last_use()->num()); |
| 862 } |
| 863 |
| 864 EmitThisPropertyStore(name); |
| 865 } |
| 866 |
| 867 |
| 868 void FastCodeGenerator::VisitThrow(Throw* expr) { |
| 869 UNREACHABLE(); |
| 870 } |
| 871 |
| 872 |
| 873 void FastCodeGenerator::VisitProperty(Property* expr) { |
| 874 ASSERT_NOT_NULL(expr->obj()->AsVariableProxy()); |
| 875 ASSERT(expr->obj()->AsVariableProxy()->var()->is_this()); |
| 876 ASSERT(expr->key()->IsPropertyName()); |
| 877 if (!destination().is(no_reg)) { |
| 878 Handle<String> name = |
| 879 Handle<String>::cast(expr->key()->AsLiteral()->handle()); |
| 880 |
| 881 Comment cmnt(masm(), ";; Load from this"); |
| 882 if (FLAG_print_ir) { |
| 883 SmartPointer<char> name_string = name->ToCString(); |
| 884 PrintF("%d: t%d = Load(this, \"%s\") // last_use(this) = %d\n", |
| 885 expr->num(), expr->num(), *name_string, |
| 886 expr->var_def()->last_use()->num()); |
| 887 } |
| 888 EmitThisPropertyLoad(name); |
| 889 } |
| 890 } |
| 891 |
| 892 |
| 893 void FastCodeGenerator::VisitCall(Call* expr) { |
| 894 UNREACHABLE(); |
| 895 } |
| 896 |
| 897 |
| 898 void FastCodeGenerator::VisitCallNew(CallNew* expr) { |
| 899 UNREACHABLE(); |
| 900 } |
| 901 |
| 902 |
| 903 void FastCodeGenerator::VisitCallRuntime(CallRuntime* expr) { |
| 904 UNREACHABLE(); |
| 905 } |
| 906 |
| 907 |
| 908 void FastCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) { |
| 909 UNREACHABLE(); |
| 910 } |
| 911 |
| 912 |
| 913 void FastCodeGenerator::VisitCountOperation(CountOperation* expr) { |
| 914 UNREACHABLE(); |
| 915 } |
| 916 |
| 917 |
| 918 void FastCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) { |
| 919 // We support limited binary operations: bitwise OR only allowed to be |
| 920 // nested on the left. |
| 921 ASSERT(expr->op() == Token::BIT_OR); |
| 922 ASSERT(expr->right()->IsLeaf()); |
| 923 |
| 924 { Register my_destination = destination(); |
| 925 set_destination(accumulator1()); |
| 926 Visit(expr->left()); |
| 927 set_destination(accumulator0()); |
| 928 Visit(expr->right()); |
| 929 set_destination(my_destination); |
| 930 } |
| 931 |
| 932 Comment cmnt(masm(), ";; BIT_OR"); |
| 933 if (FLAG_print_ir) { |
| 934 PrintF("%d: ", expr->num()); |
| 935 if (!destination().is(no_reg)) PrintF("t%d = ", expr->num()); |
| 936 PrintF("BIT_OR(t%d, t%d)\n", expr->left()->num(), expr->right()->num()); |
| 937 } |
| 938 EmitBitOr(); |
| 939 } |
| 940 |
| 941 |
| 942 void FastCodeGenerator::VisitCompareOperation(CompareOperation* expr) { |
| 943 UNREACHABLE(); |
| 944 } |
| 945 |
| 946 |
| 947 void FastCodeGenerator::VisitThisFunction(ThisFunction* expr) { |
| 948 UNREACHABLE(); |
| 949 } |
| 144 | 950 |
| 145 #undef __ | 951 #undef __ |
| 146 | 952 |
| 147 | 953 |
| 148 } } // namespace v8::internal | 954 } } // namespace v8::internal |
| OLD | NEW |