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