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