sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "codegen-inl.h" |
| 31 | #include "compiler.h" |
| 32 | #include "full-codegen.h" |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 33 | #include "macro-assembler.h" |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame] | 34 | #include "scopes.h" |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 35 | #include "stub-cache.h" |
| 36 | #include "debug.h" |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 37 | #include "liveedit.h" |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 38 | |
| 39 | namespace v8 { |
| 40 | namespace internal { |
| 41 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 42 | void BreakableStatementChecker::Check(Statement* stmt) { |
| 43 | Visit(stmt); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | void BreakableStatementChecker::Check(Expression* expr) { |
| 48 | Visit(expr); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | void BreakableStatementChecker::VisitDeclaration(Declaration* decl) { |
| 53 | } |
| 54 | |
| 55 | |
| 56 | void BreakableStatementChecker::VisitBlock(Block* stmt) { |
| 57 | } |
| 58 | |
| 59 | |
| 60 | void BreakableStatementChecker::VisitExpressionStatement( |
| 61 | ExpressionStatement* stmt) { |
| 62 | // Check if expression is breakable. |
| 63 | Visit(stmt->expression()); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) { |
| 68 | } |
| 69 | |
| 70 | |
| 71 | void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) { |
| 72 | // If the condition is breakable the if statement is breakable. |
| 73 | Visit(stmt->condition()); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void BreakableStatementChecker::VisitContinueStatement( |
| 78 | ContinueStatement* stmt) { |
| 79 | } |
| 80 | |
| 81 | |
| 82 | void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) { |
| 83 | } |
| 84 | |
| 85 | |
| 86 | void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) { |
| 87 | // Return is breakable if the expression is. |
| 88 | Visit(stmt->expression()); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void BreakableStatementChecker::VisitWithEnterStatement( |
| 93 | WithEnterStatement* stmt) { |
| 94 | Visit(stmt->expression()); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void BreakableStatementChecker::VisitWithExitStatement( |
| 99 | WithExitStatement* stmt) { |
| 100 | } |
| 101 | |
| 102 | |
| 103 | void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) { |
| 104 | // Switch statements breakable if the tag expression is. |
| 105 | Visit(stmt->tag()); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 110 | // Mark do while as breakable to avoid adding a break slot in front of it. |
| 111 | is_breakable_ = true; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) { |
| 116 | // Mark while statements breakable if the condition expression is. |
| 117 | Visit(stmt->cond()); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) { |
| 122 | // Mark for statements breakable if the condition expression is. |
| 123 | if (stmt->cond() != NULL) { |
| 124 | Visit(stmt->cond()); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) { |
| 130 | // Mark for in statements breakable if the enumerable expression is. |
| 131 | Visit(stmt->enumerable()); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | void BreakableStatementChecker::VisitTryCatchStatement( |
| 136 | TryCatchStatement* stmt) { |
| 137 | // Mark try catch as breakable to avoid adding a break slot in front of it. |
| 138 | is_breakable_ = true; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void BreakableStatementChecker::VisitTryFinallyStatement( |
| 143 | TryFinallyStatement* stmt) { |
| 144 | // Mark try finally as breakable to avoid adding a break slot in front of it. |
| 145 | is_breakable_ = true; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | void BreakableStatementChecker::VisitDebuggerStatement( |
| 150 | DebuggerStatement* stmt) { |
| 151 | // The debugger statement is breakable. |
| 152 | is_breakable_ = true; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void BreakableStatementChecker::VisitSharedFunctionInfoLiteral( |
| 161 | SharedFunctionInfoLiteral* expr) { |
| 162 | } |
| 163 | |
| 164 | |
| 165 | void BreakableStatementChecker::VisitConditional(Conditional* expr) { |
| 166 | } |
| 167 | |
| 168 | |
| 169 | void BreakableStatementChecker::VisitSlot(Slot* expr) { |
| 170 | } |
| 171 | |
| 172 | |
| 173 | void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) { |
| 174 | } |
| 175 | |
| 176 | |
| 177 | void BreakableStatementChecker::VisitLiteral(Literal* expr) { |
| 178 | } |
| 179 | |
| 180 | |
| 181 | void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 182 | } |
| 183 | |
| 184 | |
| 185 | void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) { |
| 186 | } |
| 187 | |
| 188 | |
| 189 | void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) { |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void BreakableStatementChecker::VisitCatchExtensionObject( |
| 194 | CatchExtensionObject* expr) { |
| 195 | } |
| 196 | |
| 197 | |
| 198 | void BreakableStatementChecker::VisitAssignment(Assignment* expr) { |
| 199 | // If assigning to a property (including a global property) the assignment is |
| 200 | // breakable. |
| 201 | Variable* var = expr->target()->AsVariableProxy()->AsVariable(); |
| 202 | Property* prop = expr->target()->AsProperty(); |
| 203 | if (prop != NULL || (var != NULL && var->is_global())) { |
| 204 | is_breakable_ = true; |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | // Otherwise the assignment is breakable if the assigned value is. |
| 209 | Visit(expr->value()); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | void BreakableStatementChecker::VisitThrow(Throw* expr) { |
| 214 | // Throw is breakable if the expression is. |
| 215 | Visit(expr->exception()); |
| 216 | } |
| 217 | |
| 218 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 219 | void BreakableStatementChecker::VisitIncrementOperation( |
| 220 | IncrementOperation* expr) { |
| 221 | UNREACHABLE(); |
| 222 | } |
| 223 | |
| 224 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 225 | void BreakableStatementChecker::VisitProperty(Property* expr) { |
| 226 | // Property load is breakable. |
| 227 | is_breakable_ = true; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | void BreakableStatementChecker::VisitCall(Call* expr) { |
| 232 | // Function calls both through IC and call stub are breakable. |
| 233 | is_breakable_ = true; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | void BreakableStatementChecker::VisitCallNew(CallNew* expr) { |
| 238 | // Function calls through new are breakable. |
| 239 | is_breakable_ = true; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) { |
| 244 | } |
| 245 | |
| 246 | |
| 247 | void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) { |
| 248 | Visit(expr->expression()); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) { |
| 253 | Visit(expr->expression()); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) { |
| 258 | Visit(expr->left()); |
| 259 | Visit(expr->right()); |
| 260 | } |
| 261 | |
| 262 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 263 | void BreakableStatementChecker::VisitCompareToNull(CompareToNull* expr) { |
| 264 | Visit(expr->expression()); |
| 265 | } |
| 266 | |
| 267 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 268 | void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) { |
| 269 | Visit(expr->left()); |
| 270 | Visit(expr->right()); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) { |
| 275 | } |
| 276 | |
| 277 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 278 | #define __ ACCESS_MASM(masm()) |
| 279 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame^] | 280 | bool FullCodeGenerator::MakeCode(CompilationInfo* info) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 281 | Handle<Script> script = info->script(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 282 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 283 | int len = String::cast(script->source())->length(); |
| 284 | Counters::total_full_codegen_source_size.Increment(len); |
| 285 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 286 | CodeGenerator::MakeCodePrologue(info); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 287 | const int kInitialBufferSize = 4 * KB; |
| 288 | MacroAssembler masm(NULL, kInitialBufferSize); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 289 | |
| 290 | FullCodeGenerator cgen(&masm); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 291 | cgen.Generate(info); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 292 | if (cgen.HasStackOverflow()) { |
| 293 | ASSERT(!Top::has_pending_exception()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame^] | 294 | return false; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 295 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame^] | 296 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 297 | Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame^] | 298 | Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info); |
| 299 | info->SetCode(code); // may be an empty handle. |
| 300 | return !code.is_null(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
sgjesse@chromium.org | 2ec107f | 2010-09-13 09:19:46 +0000 | [diff] [blame] | 304 | MemOperand FullCodeGenerator::ContextOperand(Register context, int index) { |
| 305 | return CodeGenerator::ContextOperand(context, index); |
| 306 | } |
| 307 | |
| 308 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 309 | int FullCodeGenerator::SlotOffset(Slot* slot) { |
| 310 | ASSERT(slot != NULL); |
| 311 | // Offset is negative because higher indexes are at lower addresses. |
| 312 | int offset = -slot->index() * kPointerSize; |
| 313 | // Adjust by a (parameter or local) base offset. |
| 314 | switch (slot->type()) { |
| 315 | case Slot::PARAMETER: |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 316 | offset += (scope()->num_parameters() + 1) * kPointerSize; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 317 | break; |
| 318 | case Slot::LOCAL: |
| 319 | offset += JavaScriptFrameConstants::kLocal0Offset; |
| 320 | break; |
| 321 | case Slot::CONTEXT: |
| 322 | case Slot::LOOKUP: |
| 323 | UNREACHABLE(); |
| 324 | } |
| 325 | return offset; |
| 326 | } |
| 327 | |
| 328 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 329 | bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 330 | // Inline smi case inside loops, but not division and modulo which |
| 331 | // are too complicated and take up too much space. |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 332 | if (op == Token::DIV ||op == Token::MOD) return false; |
| 333 | if (FLAG_always_inline_smi_code) return true; |
| 334 | return loop_depth_ > 0; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 338 | void FullCodeGenerator::EffectContext::Plug(Register reg) const { |
| 339 | } |
| 340 | |
| 341 | |
| 342 | void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const { |
| 343 | // Move value into place. |
| 344 | __ Move(result_register(), reg); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | void FullCodeGenerator::StackValueContext::Plug(Register reg) const { |
| 349 | // Move value into place. |
| 350 | __ push(reg); |
| 351 | } |
| 352 | |
| 353 | |
| 354 | void FullCodeGenerator::TestContext::Plug(Register reg) const { |
| 355 | // For simplicity we always test the accumulator register. |
| 356 | __ Move(result_register(), reg); |
| 357 | codegen()->DoTest(true_label_, false_label_, fall_through_); |
| 358 | } |
| 359 | |
| 360 | |
| 361 | void FullCodeGenerator::EffectContext::PlugTOS() const { |
| 362 | __ Drop(1); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const { |
| 367 | __ pop(result_register()); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | void FullCodeGenerator::StackValueContext::PlugTOS() const { |
| 372 | } |
| 373 | |
| 374 | |
| 375 | void FullCodeGenerator::TestContext::PlugTOS() const { |
| 376 | // For simplicity we always test the accumulator register. |
| 377 | __ pop(result_register()); |
| 378 | codegen()->DoTest(true_label_, false_label_, fall_through_); |
| 379 | } |
| 380 | |
| 381 | |
| 382 | void FullCodeGenerator::EffectContext::PrepareTest( |
| 383 | Label* materialize_true, |
| 384 | Label* materialize_false, |
| 385 | Label** if_true, |
| 386 | Label** if_false, |
| 387 | Label** fall_through) const { |
| 388 | // In an effect context, the true and the false case branch to the |
| 389 | // same label. |
| 390 | *if_true = *if_false = *fall_through = materialize_true; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | void FullCodeGenerator::AccumulatorValueContext::PrepareTest( |
| 395 | Label* materialize_true, |
| 396 | Label* materialize_false, |
| 397 | Label** if_true, |
| 398 | Label** if_false, |
| 399 | Label** fall_through) const { |
| 400 | *if_true = *fall_through = materialize_true; |
| 401 | *if_false = materialize_false; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | void FullCodeGenerator::StackValueContext::PrepareTest( |
| 406 | Label* materialize_true, |
| 407 | Label* materialize_false, |
| 408 | Label** if_true, |
| 409 | Label** if_false, |
| 410 | Label** fall_through) const { |
| 411 | *if_true = *fall_through = materialize_true; |
| 412 | *if_false = materialize_false; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | void FullCodeGenerator::TestContext::PrepareTest( |
| 417 | Label* materialize_true, |
| 418 | Label* materialize_false, |
| 419 | Label** if_true, |
| 420 | Label** if_false, |
| 421 | Label** fall_through) const { |
| 422 | *if_true = true_label_; |
| 423 | *if_false = false_label_; |
| 424 | *fall_through = fall_through_; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 428 | void FullCodeGenerator::VisitDeclarations( |
| 429 | ZoneList<Declaration*>* declarations) { |
| 430 | int length = declarations->length(); |
| 431 | int globals = 0; |
| 432 | for (int i = 0; i < length; i++) { |
| 433 | Declaration* decl = declarations->at(i); |
| 434 | Variable* var = decl->proxy()->var(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 435 | Slot* slot = var->AsSlot(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 436 | |
| 437 | // If it was not possible to allocate the variable at compile |
| 438 | // time, we need to "declare" it at runtime to make sure it |
| 439 | // actually exists in the local context. |
| 440 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 441 | VisitDeclaration(decl); |
| 442 | } else { |
| 443 | // Count global variables and functions for later processing |
| 444 | globals++; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | // Compute array of global variable and function declarations. |
| 449 | // Do nothing in case of no declared global functions or variables. |
| 450 | if (globals > 0) { |
| 451 | Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED); |
| 452 | for (int j = 0, i = 0; i < length; i++) { |
| 453 | Declaration* decl = declarations->at(i); |
| 454 | Variable* var = decl->proxy()->var(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 455 | Slot* slot = var->AsSlot(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 456 | |
| 457 | if ((slot == NULL || slot->type() != Slot::LOOKUP) && var->is_global()) { |
| 458 | array->set(j++, *(var->name())); |
| 459 | if (decl->fun() == NULL) { |
| 460 | if (var->mode() == Variable::CONST) { |
| 461 | // In case this is const property use the hole. |
| 462 | array->set_the_hole(j++); |
| 463 | } else { |
| 464 | array->set_undefined(j++); |
| 465 | } |
| 466 | } else { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 467 | Handle<SharedFunctionInfo> function = |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame^] | 468 | Compiler::BuildFunctionInfo(decl->fun(), script()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 469 | // Check for stack-overflow exception. |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame^] | 470 | if (function.is_null()) { |
| 471 | SetStackOverflow(); |
| 472 | return; |
| 473 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 474 | array->set(j++, *function); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | // Invoke the platform-dependent code generator to do the actual |
| 479 | // declaration the global variables and functions. |
| 480 | DeclareGlobals(array); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | |
| 485 | void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) { |
| 486 | if (FLAG_debug_info) { |
| 487 | CodeGenerator::RecordPositions(masm_, fun->start_position()); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | |
| 492 | void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) { |
| 493 | if (FLAG_debug_info) { |
whesse@chromium.org | e90029b | 2010-08-02 11:52:17 +0000 | [diff] [blame] | 494 | CodeGenerator::RecordPositions(masm_, fun->end_position() - 1); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | |
| 499 | void FullCodeGenerator::SetStatementPosition(Statement* stmt) { |
| 500 | if (FLAG_debug_info) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 501 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 502 | if (!Debugger::IsDebuggerActive()) { |
| 503 | CodeGenerator::RecordPositions(masm_, stmt->statement_pos()); |
| 504 | } else { |
| 505 | // Check if the statement will be breakable without adding a debug break |
| 506 | // slot. |
| 507 | BreakableStatementChecker checker; |
| 508 | checker.Check(stmt); |
| 509 | // Record the statement position right here if the statement is not |
| 510 | // breakable. For breakable statements the actual recording of the |
| 511 | // position will be postponed to the breakable code (typically an IC). |
| 512 | bool position_recorded = CodeGenerator::RecordPositions( |
| 513 | masm_, stmt->statement_pos(), !checker.is_breakable()); |
| 514 | // If the position recording did record a new position generate a debug |
| 515 | // break slot to make the statement breakable. |
| 516 | if (position_recorded) { |
| 517 | Debug::GenerateSlot(masm_); |
| 518 | } |
| 519 | } |
| 520 | #else |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 521 | CodeGenerator::RecordPositions(masm_, stmt->statement_pos()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 522 | #endif |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | |
| 527 | void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) { |
| 528 | if (FLAG_debug_info) { |
| 529 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 530 | if (!Debugger::IsDebuggerActive()) { |
| 531 | CodeGenerator::RecordPositions(masm_, pos); |
| 532 | } else { |
| 533 | // Check if the expression will be breakable without adding a debug break |
| 534 | // slot. |
| 535 | BreakableStatementChecker checker; |
| 536 | checker.Check(expr); |
| 537 | // Record a statement position right here if the expression is not |
| 538 | // breakable. For breakable expressions the actual recording of the |
| 539 | // position will be postponed to the breakable code (typically an IC). |
| 540 | // NOTE this will record a statement position for something which might |
| 541 | // not be a statement. As stepping in the debugger will only stop at |
| 542 | // statement positions this is used for e.g. the condition expression of |
| 543 | // a do while loop. |
| 544 | bool position_recorded = CodeGenerator::RecordPositions( |
| 545 | masm_, pos, !checker.is_breakable()); |
| 546 | // If the position recording did record a new position generate a debug |
| 547 | // break slot to make the statement breakable. |
| 548 | if (position_recorded) { |
| 549 | Debug::GenerateSlot(masm_); |
| 550 | } |
| 551 | } |
| 552 | #else |
| 553 | CodeGenerator::RecordPositions(masm_, pos); |
| 554 | #endif |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
| 558 | |
| 559 | void FullCodeGenerator::SetStatementPosition(int pos) { |
| 560 | if (FLAG_debug_info) { |
| 561 | CodeGenerator::RecordPositions(masm_, pos); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | |
| 566 | void FullCodeGenerator::SetSourcePosition(int pos) { |
| 567 | if (FLAG_debug_info && pos != RelocInfo::kNoPosition) { |
| 568 | masm_->RecordPosition(pos); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 573 | // Lookup table for code generators for special runtime calls which are |
| 574 | // generated inline. |
| 575 | #define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \ |
| 576 | &FullCodeGenerator::Emit##Name, |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 577 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 578 | const FullCodeGenerator::InlineFunctionGenerator |
| 579 | FullCodeGenerator::kInlineFunctionGenerators[] = { |
| 580 | INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 581 | INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 582 | }; |
| 583 | #undef INLINE_FUNCTION_GENERATOR_ADDRESS |
| 584 | |
| 585 | |
| 586 | FullCodeGenerator::InlineFunctionGenerator |
| 587 | FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) { |
| 588 | return kInlineFunctionGenerators[ |
| 589 | static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction)]; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* node) { |
| 594 | ZoneList<Expression*>* args = node->arguments(); |
| 595 | Handle<String> name = node->name(); |
| 596 | Runtime::Function* function = node->function(); |
| 597 | ASSERT(function != NULL); |
| 598 | ASSERT(function->intrinsic_type == Runtime::INLINE); |
| 599 | InlineFunctionGenerator generator = |
| 600 | FindInlineFunctionGenerator(function->function_id); |
| 601 | ASSERT(generator != NULL); |
| 602 | ((*this).*(generator))(args); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | |
| 606 | void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) { |
| 607 | Comment cmnt(masm_, "[ BinaryOperation"); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 608 | Token::Value op = expr->op(); |
| 609 | Expression* left = expr->left(); |
| 610 | Expression* right = expr->right(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 611 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 612 | OverwriteMode mode = NO_OVERWRITE; |
| 613 | if (left->ResultOverwriteAllowed()) { |
| 614 | mode = OVERWRITE_LEFT; |
| 615 | } else if (right->ResultOverwriteAllowed()) { |
| 616 | mode = OVERWRITE_RIGHT; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 617 | } |
| 618 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 619 | switch (op) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 620 | case Token::COMMA: |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 621 | VisitForEffect(left); |
| 622 | Visit(right); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 623 | break; |
| 624 | |
| 625 | case Token::OR: |
| 626 | case Token::AND: |
| 627 | EmitLogicalOperation(expr); |
| 628 | break; |
| 629 | |
| 630 | case Token::ADD: |
| 631 | case Token::SUB: |
| 632 | case Token::DIV: |
| 633 | case Token::MOD: |
| 634 | case Token::MUL: |
| 635 | case Token::BIT_OR: |
| 636 | case Token::BIT_AND: |
| 637 | case Token::BIT_XOR: |
| 638 | case Token::SHL: |
| 639 | case Token::SHR: |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 640 | case Token::SAR: { |
| 641 | // Figure out if either of the operands is a constant. |
| 642 | ConstantOperand constant = ShouldInlineSmiCase(op) |
| 643 | ? GetConstantOperand(op, left, right) |
| 644 | : kNoConstants; |
| 645 | |
| 646 | // Load only the operands that we need to materialize. |
| 647 | if (constant == kNoConstants) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 648 | VisitForStackValue(left); |
| 649 | VisitForAccumulatorValue(right); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 650 | } else if (constant == kRightConstant) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 651 | VisitForAccumulatorValue(left); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 652 | } else { |
| 653 | ASSERT(constant == kLeftConstant); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 654 | VisitForAccumulatorValue(right); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 655 | } |
| 656 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 657 | SetSourcePosition(expr->position()); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 658 | if (ShouldInlineSmiCase(op)) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 659 | EmitInlineSmiBinaryOp(expr, op, mode, left, right, constant); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 660 | } else { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 661 | EmitBinaryOp(op, mode); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 662 | } |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 663 | break; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 664 | } |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 665 | |
| 666 | default: |
| 667 | UNREACHABLE(); |
| 668 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 672 | void FullCodeGenerator::EmitLogicalOperation(BinaryOperation* expr) { |
| 673 | Label eval_right, done; |
| 674 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 675 | context()->EmitLogicalLeft(expr, &eval_right, &done); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 676 | |
| 677 | __ bind(&eval_right); |
| 678 | Visit(expr->right()); |
| 679 | |
| 680 | __ bind(&done); |
| 681 | } |
| 682 | |
| 683 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 684 | void FullCodeGenerator::EffectContext::EmitLogicalLeft(BinaryOperation* expr, |
| 685 | Label* eval_right, |
| 686 | Label* done) const { |
| 687 | if (expr->op() == Token::OR) { |
| 688 | codegen()->VisitForControl(expr->left(), done, eval_right, eval_right); |
| 689 | } else { |
| 690 | ASSERT(expr->op() == Token::AND); |
| 691 | codegen()->VisitForControl(expr->left(), eval_right, done, eval_right); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 692 | } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 693 | } |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 694 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 695 | |
| 696 | void FullCodeGenerator::AccumulatorValueContext::EmitLogicalLeft( |
| 697 | BinaryOperation* expr, |
| 698 | Label* eval_right, |
| 699 | Label* done) const { |
| 700 | codegen()->Visit(expr->left()); |
| 701 | // We want the value in the accumulator for the test, and on the stack in case |
| 702 | // we need it. |
| 703 | __ push(result_register()); |
| 704 | Label discard, restore; |
| 705 | if (expr->op() == Token::OR) { |
| 706 | codegen()->DoTest(&restore, &discard, &restore); |
| 707 | } else { |
| 708 | ASSERT(expr->op() == Token::AND); |
| 709 | codegen()->DoTest(&discard, &restore, &restore); |
| 710 | } |
| 711 | __ bind(&restore); |
| 712 | __ pop(result_register()); |
| 713 | __ jmp(done); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 714 | __ bind(&discard); |
| 715 | __ Drop(1); |
| 716 | } |
| 717 | |
| 718 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 719 | void FullCodeGenerator::StackValueContext::EmitLogicalLeft( |
| 720 | BinaryOperation* expr, |
| 721 | Label* eval_right, |
| 722 | Label* done) const { |
| 723 | codegen()->VisitForAccumulatorValue(expr->left()); |
| 724 | // We want the value in the accumulator for the test, and on the stack in case |
| 725 | // we need it. |
| 726 | __ push(result_register()); |
| 727 | Label discard; |
| 728 | if (expr->op() == Token::OR) { |
| 729 | codegen()->DoTest(done, &discard, &discard); |
| 730 | } else { |
| 731 | ASSERT(expr->op() == Token::AND); |
| 732 | codegen()->DoTest(&discard, done, &discard); |
| 733 | } |
| 734 | __ bind(&discard); |
| 735 | __ Drop(1); |
| 736 | } |
| 737 | |
| 738 | |
| 739 | void FullCodeGenerator::TestContext::EmitLogicalLeft(BinaryOperation* expr, |
| 740 | Label* eval_right, |
| 741 | Label* done) const { |
| 742 | if (expr->op() == Token::OR) { |
| 743 | codegen()->VisitForControl(expr->left(), |
| 744 | true_label_, eval_right, eval_right); |
| 745 | } else { |
| 746 | ASSERT(expr->op() == Token::AND); |
| 747 | codegen()->VisitForControl(expr->left(), |
| 748 | eval_right, false_label_, eval_right); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 753 | void FullCodeGenerator::VisitBlock(Block* stmt) { |
| 754 | Comment cmnt(masm_, "[ Block"); |
| 755 | Breakable nested_statement(this, stmt); |
| 756 | SetStatementPosition(stmt); |
| 757 | VisitStatements(stmt->statements()); |
| 758 | __ bind(nested_statement.break_target()); |
| 759 | } |
| 760 | |
| 761 | |
| 762 | void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) { |
| 763 | Comment cmnt(masm_, "[ ExpressionStatement"); |
| 764 | SetStatementPosition(stmt); |
| 765 | VisitForEffect(stmt->expression()); |
| 766 | } |
| 767 | |
| 768 | |
| 769 | void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) { |
| 770 | Comment cmnt(masm_, "[ EmptyStatement"); |
| 771 | SetStatementPosition(stmt); |
| 772 | } |
| 773 | |
| 774 | |
| 775 | void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) { |
| 776 | Comment cmnt(masm_, "[ IfStatement"); |
| 777 | SetStatementPosition(stmt); |
| 778 | Label then_part, else_part, done; |
| 779 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 780 | if (stmt->HasElseStatement()) { |
| 781 | VisitForControl(stmt->condition(), &then_part, &else_part, &then_part); |
| 782 | __ bind(&then_part); |
| 783 | Visit(stmt->then_statement()); |
| 784 | __ jmp(&done); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 785 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 786 | __ bind(&else_part); |
| 787 | Visit(stmt->else_statement()); |
| 788 | } else { |
| 789 | VisitForControl(stmt->condition(), &then_part, &done, &then_part); |
| 790 | __ bind(&then_part); |
| 791 | Visit(stmt->then_statement()); |
| 792 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 793 | __ bind(&done); |
| 794 | } |
| 795 | |
| 796 | |
| 797 | void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) { |
| 798 | Comment cmnt(masm_, "[ ContinueStatement"); |
| 799 | SetStatementPosition(stmt); |
| 800 | NestedStatement* current = nesting_stack_; |
| 801 | int stack_depth = 0; |
| 802 | while (!current->IsContinueTarget(stmt->target())) { |
| 803 | stack_depth = current->Exit(stack_depth); |
| 804 | current = current->outer(); |
| 805 | } |
| 806 | __ Drop(stack_depth); |
| 807 | |
| 808 | Iteration* loop = current->AsIteration(); |
| 809 | __ jmp(loop->continue_target()); |
| 810 | } |
| 811 | |
| 812 | |
| 813 | void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) { |
| 814 | Comment cmnt(masm_, "[ BreakStatement"); |
| 815 | SetStatementPosition(stmt); |
| 816 | NestedStatement* current = nesting_stack_; |
| 817 | int stack_depth = 0; |
| 818 | while (!current->IsBreakTarget(stmt->target())) { |
| 819 | stack_depth = current->Exit(stack_depth); |
| 820 | current = current->outer(); |
| 821 | } |
| 822 | __ Drop(stack_depth); |
| 823 | |
| 824 | Breakable* target = current->AsBreakable(); |
| 825 | __ jmp(target->break_target()); |
| 826 | } |
| 827 | |
| 828 | |
| 829 | void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) { |
| 830 | Comment cmnt(masm_, "[ ReturnStatement"); |
| 831 | SetStatementPosition(stmt); |
| 832 | Expression* expr = stmt->expression(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 833 | VisitForAccumulatorValue(expr); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 834 | |
| 835 | // Exit all nested statements. |
| 836 | NestedStatement* current = nesting_stack_; |
| 837 | int stack_depth = 0; |
| 838 | while (current != NULL) { |
| 839 | stack_depth = current->Exit(stack_depth); |
| 840 | current = current->outer(); |
| 841 | } |
| 842 | __ Drop(stack_depth); |
| 843 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 844 | EmitReturnSequence(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | |
| 848 | void FullCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) { |
| 849 | Comment cmnt(masm_, "[ WithEnterStatement"); |
| 850 | SetStatementPosition(stmt); |
| 851 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 852 | VisitForStackValue(stmt->expression()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 853 | if (stmt->is_catch_block()) { |
| 854 | __ CallRuntime(Runtime::kPushCatchContext, 1); |
| 855 | } else { |
| 856 | __ CallRuntime(Runtime::kPushContext, 1); |
| 857 | } |
| 858 | // Both runtime calls return the new context in both the context and the |
| 859 | // result registers. |
| 860 | |
| 861 | // Update local stack frame context field. |
| 862 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
| 863 | } |
| 864 | |
| 865 | |
| 866 | void FullCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) { |
| 867 | Comment cmnt(masm_, "[ WithExitStatement"); |
| 868 | SetStatementPosition(stmt); |
| 869 | |
| 870 | // Pop context. |
| 871 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 872 | // Update local stack frame context field. |
| 873 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
| 874 | } |
| 875 | |
| 876 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 877 | void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 878 | Comment cmnt(masm_, "[ DoWhileStatement"); |
| 879 | SetStatementPosition(stmt); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 880 | Label body, stack_limit_hit, stack_check_success, done; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 881 | |
| 882 | Iteration loop_statement(this, stmt); |
| 883 | increment_loop_depth(); |
| 884 | |
| 885 | __ bind(&body); |
| 886 | Visit(stmt->body()); |
| 887 | |
| 888 | // Check stack before looping. |
| 889 | __ StackLimitCheck(&stack_limit_hit); |
| 890 | __ bind(&stack_check_success); |
| 891 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 892 | // Record the position of the do while condition and make sure it is |
| 893 | // possible to break on the condition. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 894 | __ bind(loop_statement.continue_target()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 895 | SetExpressionPosition(stmt->cond(), stmt->condition_position()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 896 | VisitForControl(stmt->cond(), |
| 897 | &body, |
| 898 | loop_statement.break_target(), |
| 899 | loop_statement.break_target()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 900 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 901 | __ bind(loop_statement.break_target()); |
| 902 | __ jmp(&done); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 903 | |
| 904 | __ bind(&stack_limit_hit); |
| 905 | StackCheckStub stack_stub; |
| 906 | __ CallStub(&stack_stub); |
| 907 | __ jmp(&stack_check_success); |
| 908 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 909 | __ bind(&done); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 910 | decrement_loop_depth(); |
| 911 | } |
| 912 | |
| 913 | |
| 914 | void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) { |
| 915 | Comment cmnt(masm_, "[ WhileStatement"); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 916 | Label body, stack_limit_hit, stack_check_success, done; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 917 | |
| 918 | Iteration loop_statement(this, stmt); |
| 919 | increment_loop_depth(); |
| 920 | |
| 921 | // Emit the test at the bottom of the loop. |
| 922 | __ jmp(loop_statement.continue_target()); |
| 923 | |
| 924 | __ bind(&body); |
| 925 | Visit(stmt->body()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 926 | __ bind(loop_statement.continue_target()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 927 | |
| 928 | // Emit the statement position here as this is where the while |
| 929 | // statement code starts. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 930 | SetStatementPosition(stmt); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 931 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 932 | // Check stack before looping. |
| 933 | __ StackLimitCheck(&stack_limit_hit); |
| 934 | __ bind(&stack_check_success); |
| 935 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 936 | VisitForControl(stmt->cond(), |
| 937 | &body, |
| 938 | loop_statement.break_target(), |
| 939 | loop_statement.break_target()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 940 | |
| 941 | __ bind(loop_statement.break_target()); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 942 | __ jmp(&done); |
| 943 | |
| 944 | __ bind(&stack_limit_hit); |
| 945 | StackCheckStub stack_stub; |
| 946 | __ CallStub(&stack_stub); |
| 947 | __ jmp(&stack_check_success); |
| 948 | |
| 949 | __ bind(&done); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 950 | decrement_loop_depth(); |
| 951 | } |
| 952 | |
| 953 | |
| 954 | void FullCodeGenerator::VisitForStatement(ForStatement* stmt) { |
| 955 | Comment cmnt(masm_, "[ ForStatement"); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 956 | Label test, body, stack_limit_hit, stack_check_success; |
| 957 | |
| 958 | Iteration loop_statement(this, stmt); |
| 959 | if (stmt->init() != NULL) { |
| 960 | Visit(stmt->init()); |
| 961 | } |
| 962 | |
| 963 | increment_loop_depth(); |
| 964 | // Emit the test at the bottom of the loop (even if empty). |
| 965 | __ jmp(&test); |
| 966 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 967 | __ bind(&stack_limit_hit); |
| 968 | StackCheckStub stack_stub; |
| 969 | __ CallStub(&stack_stub); |
| 970 | __ jmp(&stack_check_success); |
| 971 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 972 | __ bind(&body); |
| 973 | Visit(stmt->body()); |
| 974 | |
| 975 | __ bind(loop_statement.continue_target()); |
| 976 | |
| 977 | SetStatementPosition(stmt); |
| 978 | if (stmt->next() != NULL) { |
| 979 | Visit(stmt->next()); |
| 980 | } |
| 981 | |
| 982 | __ bind(&test); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 983 | // Emit the statement position here as this is where the for |
| 984 | // statement code starts. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 985 | SetStatementPosition(stmt); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 986 | |
| 987 | // Check stack before looping. |
| 988 | __ StackLimitCheck(&stack_limit_hit); |
| 989 | __ bind(&stack_check_success); |
| 990 | |
| 991 | if (stmt->cond() != NULL) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 992 | VisitForControl(stmt->cond(), |
| 993 | &body, |
| 994 | loop_statement.break_target(), |
| 995 | loop_statement.break_target()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 996 | } else { |
| 997 | __ jmp(&body); |
| 998 | } |
| 999 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1000 | __ bind(loop_statement.break_target()); |
| 1001 | decrement_loop_depth(); |
| 1002 | } |
| 1003 | |
| 1004 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1005 | void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 1006 | Comment cmnt(masm_, "[ TryCatchStatement"); |
| 1007 | SetStatementPosition(stmt); |
| 1008 | // The try block adds a handler to the exception handler chain |
| 1009 | // before entering, and removes it again when exiting normally. |
| 1010 | // If an exception is thrown during execution of the try block, |
| 1011 | // control is passed to the handler, which also consumes the handler. |
| 1012 | // At this point, the exception is in a register, and store it in |
| 1013 | // the temporary local variable (prints as ".catch-var") before |
| 1014 | // executing the catch block. The catch block has been rewritten |
| 1015 | // to introduce a new scope to bind the catch variable and to remove |
| 1016 | // that scope again afterwards. |
| 1017 | |
| 1018 | Label try_handler_setup, catch_entry, done; |
| 1019 | __ Call(&try_handler_setup); |
| 1020 | // Try handler code, exception in result register. |
| 1021 | |
| 1022 | // Store exception in local .catch variable before executing catch block. |
| 1023 | { |
| 1024 | // The catch variable is *always* a variable proxy for a local variable. |
| 1025 | Variable* catch_var = stmt->catch_var()->AsVariableProxy()->AsVariable(); |
| 1026 | ASSERT_NOT_NULL(catch_var); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1027 | Slot* variable_slot = catch_var->AsSlot(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1028 | ASSERT_NOT_NULL(variable_slot); |
| 1029 | ASSERT_EQ(Slot::LOCAL, variable_slot->type()); |
| 1030 | StoreToFrameField(SlotOffset(variable_slot), result_register()); |
| 1031 | } |
| 1032 | |
| 1033 | Visit(stmt->catch_block()); |
| 1034 | __ jmp(&done); |
| 1035 | |
| 1036 | // Try block code. Sets up the exception handler chain. |
| 1037 | __ bind(&try_handler_setup); |
| 1038 | { |
| 1039 | TryCatch try_block(this, &catch_entry); |
| 1040 | __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER); |
| 1041 | Visit(stmt->try_block()); |
| 1042 | __ PopTryHandler(); |
| 1043 | } |
| 1044 | __ bind(&done); |
| 1045 | } |
| 1046 | |
| 1047 | |
| 1048 | void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
| 1049 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
| 1050 | SetStatementPosition(stmt); |
| 1051 | // Try finally is compiled by setting up a try-handler on the stack while |
| 1052 | // executing the try body, and removing it again afterwards. |
| 1053 | // |
| 1054 | // The try-finally construct can enter the finally block in three ways: |
| 1055 | // 1. By exiting the try-block normally. This removes the try-handler and |
| 1056 | // calls the finally block code before continuing. |
| 1057 | // 2. By exiting the try-block with a function-local control flow transfer |
| 1058 | // (break/continue/return). The site of the, e.g., break removes the |
| 1059 | // try handler and calls the finally block code before continuing |
| 1060 | // its outward control transfer. |
| 1061 | // 3. by exiting the try-block with a thrown exception. |
| 1062 | // This can happen in nested function calls. It traverses the try-handler |
| 1063 | // chain and consumes the try-handler entry before jumping to the |
| 1064 | // handler code. The handler code then calls the finally-block before |
| 1065 | // rethrowing the exception. |
| 1066 | // |
| 1067 | // The finally block must assume a return address on top of the stack |
| 1068 | // (or in the link register on ARM chips) and a value (return value or |
| 1069 | // exception) in the result register (rax/eax/r0), both of which must |
| 1070 | // be preserved. The return address isn't GC-safe, so it should be |
| 1071 | // cooked before GC. |
| 1072 | Label finally_entry; |
| 1073 | Label try_handler_setup; |
| 1074 | |
| 1075 | // Setup the try-handler chain. Use a call to |
| 1076 | // Jump to try-handler setup and try-block code. Use call to put try-handler |
| 1077 | // address on stack. |
| 1078 | __ Call(&try_handler_setup); |
| 1079 | // Try handler code. Return address of call is pushed on handler stack. |
| 1080 | { |
| 1081 | // This code is only executed during stack-handler traversal when an |
| 1082 | // exception is thrown. The execption is in the result register, which |
| 1083 | // is retained by the finally block. |
| 1084 | // Call the finally block and then rethrow the exception. |
| 1085 | __ Call(&finally_entry); |
| 1086 | __ push(result_register()); |
| 1087 | __ CallRuntime(Runtime::kReThrow, 1); |
| 1088 | } |
| 1089 | |
| 1090 | __ bind(&finally_entry); |
| 1091 | { |
| 1092 | // Finally block implementation. |
| 1093 | Finally finally_block(this); |
| 1094 | EnterFinallyBlock(); |
| 1095 | Visit(stmt->finally_block()); |
| 1096 | ExitFinallyBlock(); // Return to the calling code. |
| 1097 | } |
| 1098 | |
| 1099 | __ bind(&try_handler_setup); |
| 1100 | { |
| 1101 | // Setup try handler (stack pointer registers). |
| 1102 | TryFinally try_block(this, &finally_entry); |
| 1103 | __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER); |
| 1104 | Visit(stmt->try_block()); |
| 1105 | __ PopTryHandler(); |
| 1106 | } |
| 1107 | // Execute the finally block on the way out. |
| 1108 | __ Call(&finally_entry); |
| 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) { |
| 1113 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1114 | Comment cmnt(masm_, "[ DebuggerStatement"); |
| 1115 | SetStatementPosition(stmt); |
| 1116 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1117 | __ DebugBreak(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1118 | // Ignore the return value. |
| 1119 | #endif |
| 1120 | } |
| 1121 | |
| 1122 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1123 | void FullCodeGenerator::VisitConditional(Conditional* expr) { |
| 1124 | Comment cmnt(masm_, "[ Conditional"); |
| 1125 | Label true_case, false_case, done; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1126 | VisitForControl(expr->condition(), &true_case, &false_case, &true_case); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1127 | |
| 1128 | __ bind(&true_case); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1129 | SetExpressionPosition(expr->then_expression(), |
| 1130 | expr->then_expression_position()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame^] | 1131 | if (context()->IsTest()) { |
| 1132 | const TestContext* for_test = TestContext::cast(context()); |
| 1133 | VisitForControl(expr->then_expression(), |
| 1134 | for_test->true_label(), |
| 1135 | for_test->false_label(), |
| 1136 | NULL); |
| 1137 | } else { |
| 1138 | Visit(expr->then_expression()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1139 | __ jmp(&done); |
| 1140 | } |
| 1141 | |
| 1142 | __ bind(&false_case); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1143 | SetExpressionPosition(expr->else_expression(), |
| 1144 | expr->else_expression_position()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1145 | Visit(expr->else_expression()); |
| 1146 | // If control flow falls through Visit, merge it with true case here. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1147 | if (!context()->IsTest()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1148 | __ bind(&done); |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | |
| 1153 | void FullCodeGenerator::VisitSlot(Slot* expr) { |
| 1154 | // Slots do not appear directly in the AST. |
| 1155 | UNREACHABLE(); |
| 1156 | } |
| 1157 | |
| 1158 | |
| 1159 | void FullCodeGenerator::VisitLiteral(Literal* expr) { |
| 1160 | Comment cmnt(masm_, "[ Literal"); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1161 | context()->Plug(expr->handle()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1165 | void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 1166 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 1167 | |
| 1168 | // Build the function boilerplate and instantiate it. |
| 1169 | Handle<SharedFunctionInfo> function_info = |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame^] | 1170 | Compiler::BuildFunctionInfo(expr, script()); |
| 1171 | if (function_info.is_null()) { |
| 1172 | SetStackOverflow(); |
| 1173 | return; |
| 1174 | } |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1175 | EmitNewClosure(function_info); |
| 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | void FullCodeGenerator::VisitSharedFunctionInfoLiteral( |
| 1180 | SharedFunctionInfoLiteral* expr) { |
| 1181 | Comment cmnt(masm_, "[ SharedFunctionInfoLiteral"); |
| 1182 | EmitNewClosure(expr->shared_function_info()); |
| 1183 | } |
| 1184 | |
| 1185 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1186 | void FullCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) { |
| 1187 | // Call runtime routine to allocate the catch extension object and |
| 1188 | // assign the exception value to the catch variable. |
| 1189 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1190 | VisitForStackValue(expr->key()); |
| 1191 | VisitForStackValue(expr->value()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1192 | // Create catch extension object. |
| 1193 | __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1194 | context()->Plug(result_register()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | |
| 1198 | void FullCodeGenerator::VisitThrow(Throw* expr) { |
| 1199 | Comment cmnt(masm_, "[ Throw"); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1200 | VisitForStackValue(expr->exception()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1201 | __ CallRuntime(Runtime::kThrow, 1); |
| 1202 | // Never returns here. |
| 1203 | } |
| 1204 | |
| 1205 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1206 | void FullCodeGenerator::VisitIncrementOperation(IncrementOperation* expr) { |
| 1207 | UNREACHABLE(); |
| 1208 | } |
| 1209 | |
| 1210 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1211 | int FullCodeGenerator::TryFinally::Exit(int stack_depth) { |
| 1212 | // The macros used here must preserve the result register. |
| 1213 | __ Drop(stack_depth); |
| 1214 | __ PopTryHandler(); |
| 1215 | __ Call(finally_entry_); |
| 1216 | return 0; |
| 1217 | } |
| 1218 | |
| 1219 | |
| 1220 | int FullCodeGenerator::TryCatch::Exit(int stack_depth) { |
| 1221 | // The macros used here must preserve the result register. |
| 1222 | __ Drop(stack_depth); |
| 1223 | __ PopTryHandler(); |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1227 | |
| 1228 | void FullCodeGenerator::EmitRegExpCloneResult(ZoneList<Expression*>* args) { |
| 1229 | ASSERT(args->length() == 1); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1230 | VisitForStackValue(args->at(0)); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1231 | __ CallRuntime(Runtime::kRegExpCloneResult, 1); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1232 | context()->Plug(result_register()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1235 | #undef __ |
| 1236 | |
| 1237 | |
| 1238 | } } // namespace v8::internal |