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