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