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