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