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