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