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) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 571 | int save_global_count = global_count_; |
| 572 | global_count_ = 0; |
| 573 | |
| 574 | AstVisitor::VisitDeclarations(declarations); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 575 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 576 | // Batch declare global functions and variables. |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 577 | if (global_count_ > 0) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 578 | Handle<FixedArray> array = |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 579 | isolate()->factory()->NewFixedArray(2 * global_count_, TENURED); |
| 580 | int length = declarations->length(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 581 | for (int j = 0, i = 0; i < length; i++) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 582 | Declaration* decl = declarations->at(i); |
| 583 | Variable* var = decl->proxy()->var(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 584 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 585 | if (var->IsUnallocated()) { |
| 586 | array->set(j++, *(var->name())); |
| 587 | FunctionDeclaration* fun_decl = decl->AsFunctionDeclaration(); |
| 588 | if (fun_decl == NULL) { |
| 589 | if (var->binding_needs_init()) { |
| 590 | // In case this binding needs initialization use the hole. |
| 591 | array->set_the_hole(j++); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 592 | } else { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 593 | array->set_undefined(j++); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 594 | } |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 595 | } else { |
| 596 | Handle<SharedFunctionInfo> function = |
| 597 | Compiler::BuildFunctionInfo(fun_decl->fun(), script()); |
| 598 | // Check for stack-overflow exception. |
| 599 | if (function.is_null()) { |
| 600 | SetStackOverflow(); |
| 601 | return; |
| 602 | } |
| 603 | array->set(j++, *function); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | } |
| 607 | // Invoke the platform-dependent code generator to do the actual |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 608 | // declaration the global functions and variables. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 609 | DeclareGlobals(array); |
| 610 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 611 | |
| 612 | global_count_ = save_global_count; |
| 613 | } |
| 614 | |
| 615 | |
| 616 | void FullCodeGenerator::VisitVariableDeclaration(VariableDeclaration* decl) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 617 | EmitDeclaration(decl->proxy(), decl->mode(), NULL); |
| 618 | } |
| 619 | |
| 620 | |
| 621 | void FullCodeGenerator::VisitFunctionDeclaration(FunctionDeclaration* decl) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 622 | EmitDeclaration(decl->proxy(), decl->mode(), decl->fun()); |
| 623 | } |
| 624 | |
| 625 | |
| 626 | void FullCodeGenerator::VisitModuleDeclaration(ModuleDeclaration* decl) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 627 | EmitDeclaration(decl->proxy(), decl->mode(), NULL); |
| 628 | } |
| 629 | |
| 630 | |
| 631 | void FullCodeGenerator::VisitImportDeclaration(ImportDeclaration* decl) { |
| 632 | EmitDeclaration(decl->proxy(), decl->mode(), NULL); |
| 633 | } |
| 634 | |
| 635 | |
| 636 | void FullCodeGenerator::VisitExportDeclaration(ExportDeclaration* decl) { |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 637 | // TODO(rossberg) |
| 638 | } |
| 639 | |
| 640 | |
| 641 | void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) { |
| 642 | // TODO(rossberg) |
| 643 | } |
| 644 | |
| 645 | |
| 646 | void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) { |
| 647 | // TODO(rossberg) |
| 648 | } |
| 649 | |
| 650 | |
| 651 | void FullCodeGenerator::VisitModulePath(ModulePath* module) { |
| 652 | // TODO(rossberg) |
| 653 | } |
| 654 | |
| 655 | |
| 656 | void FullCodeGenerator::VisitModuleUrl(ModuleUrl* decl) { |
| 657 | // TODO(rossberg) |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 661 | int FullCodeGenerator::DeclareGlobalsFlags() { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 662 | ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode())); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 663 | return DeclareGlobalsEvalFlag::encode(is_eval()) | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 664 | DeclareGlobalsNativeFlag::encode(is_native()) | |
| 665 | DeclareGlobalsLanguageMode::encode(language_mode()); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 669 | void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 670 | CodeGenerator::RecordPositions(masm_, fun->start_position()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | |
| 674 | void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 675 | CodeGenerator::RecordPositions(masm_, fun->end_position() - 1); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | |
| 679 | void FullCodeGenerator::SetStatementPosition(Statement* stmt) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 680 | #ifdef ENABLE_DEBUGGER_SUPPORT |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 681 | if (!isolate()->debugger()->IsDebuggerActive()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 682 | CodeGenerator::RecordPositions(masm_, stmt->statement_pos()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 683 | } else { |
| 684 | // Check if the statement will be breakable without adding a debug break |
| 685 | // slot. |
| 686 | BreakableStatementChecker checker; |
| 687 | checker.Check(stmt); |
| 688 | // Record the statement position right here if the statement is not |
| 689 | // breakable. For breakable statements the actual recording of the |
| 690 | // position will be postponed to the breakable code (typically an IC). |
| 691 | bool position_recorded = CodeGenerator::RecordPositions( |
| 692 | masm_, stmt->statement_pos(), !checker.is_breakable()); |
| 693 | // If the position recording did record a new position generate a debug |
| 694 | // break slot to make the statement breakable. |
| 695 | if (position_recorded) { |
| 696 | Debug::GenerateSlot(masm_); |
| 697 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 698 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 699 | #else |
| 700 | CodeGenerator::RecordPositions(masm_, stmt->statement_pos()); |
| 701 | #endif |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | |
| 705 | void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 706 | #ifdef ENABLE_DEBUGGER_SUPPORT |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 707 | if (!isolate()->debugger()->IsDebuggerActive()) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 708 | CodeGenerator::RecordPositions(masm_, pos); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 709 | } else { |
| 710 | // Check if the expression will be breakable without adding a debug break |
| 711 | // slot. |
| 712 | BreakableStatementChecker checker; |
| 713 | checker.Check(expr); |
| 714 | // Record a statement position right here if the expression is not |
| 715 | // breakable. For breakable expressions the actual recording of the |
| 716 | // position will be postponed to the breakable code (typically an IC). |
| 717 | // NOTE this will record a statement position for something which might |
| 718 | // not be a statement. As stepping in the debugger will only stop at |
| 719 | // statement positions this is used for e.g. the condition expression of |
| 720 | // a do while loop. |
| 721 | bool position_recorded = CodeGenerator::RecordPositions( |
| 722 | masm_, pos, !checker.is_breakable()); |
| 723 | // If the position recording did record a new position generate a debug |
| 724 | // break slot to make the statement breakable. |
| 725 | if (position_recorded) { |
| 726 | Debug::GenerateSlot(masm_); |
| 727 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 728 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 729 | #else |
| 730 | CodeGenerator::RecordPositions(masm_, pos); |
| 731 | #endif |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | |
| 735 | void FullCodeGenerator::SetStatementPosition(int pos) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 736 | CodeGenerator::RecordPositions(masm_, pos); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 740 | void FullCodeGenerator::SetSourcePosition(int pos) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 741 | if (pos != RelocInfo::kNoPosition) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 742 | masm_->positions_recorder()->RecordPosition(pos); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
| 746 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 747 | // Lookup table for code generators for special runtime calls which are |
| 748 | // generated inline. |
| 749 | #define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \ |
| 750 | &FullCodeGenerator::Emit##Name, |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 751 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 752 | const FullCodeGenerator::InlineFunctionGenerator |
| 753 | FullCodeGenerator::kInlineFunctionGenerators[] = { |
| 754 | INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 755 | INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 756 | }; |
| 757 | #undef INLINE_FUNCTION_GENERATOR_ADDRESS |
| 758 | |
| 759 | |
| 760 | FullCodeGenerator::InlineFunctionGenerator |
| 761 | FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 762 | int lookup_index = |
| 763 | static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction); |
| 764 | ASSERT(lookup_index >= 0); |
| 765 | ASSERT(static_cast<size_t>(lookup_index) < |
| 766 | ARRAY_SIZE(kInlineFunctionGenerators)); |
| 767 | return kInlineFunctionGenerators[lookup_index]; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 771 | void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) { |
| 772 | const Runtime::Function* function = expr->function(); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 773 | ASSERT(function != NULL); |
| 774 | ASSERT(function->intrinsic_type == Runtime::INLINE); |
| 775 | InlineFunctionGenerator generator = |
| 776 | FindInlineFunctionGenerator(function->function_id); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 777 | ((*this).*(generator))(expr); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | |
| 781 | void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 782 | switch (expr->op()) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 783 | case Token::COMMA: |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 784 | return VisitComma(expr); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 785 | case Token::OR: |
| 786 | case Token::AND: |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 787 | return VisitLogicalExpression(expr); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 788 | default: |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 789 | return VisitArithmeticExpression(expr); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 790 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 794 | void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) { |
| 795 | if (context()->IsEffect()) { |
| 796 | VisitForEffect(expr); |
| 797 | } else if (context()->IsAccumulatorValue()) { |
| 798 | VisitForAccumulatorValue(expr); |
| 799 | } else if (context()->IsStackValue()) { |
| 800 | VisitForStackValue(expr); |
| 801 | } else if (context()->IsTest()) { |
| 802 | const TestContext* test = TestContext::cast(context()); |
| 803 | VisitForControl(expr, test->true_label(), test->false_label(), |
| 804 | test->fall_through()); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 809 | void FullCodeGenerator::VisitComma(BinaryOperation* expr) { |
| 810 | Comment cmnt(masm_, "[ Comma"); |
| 811 | VisitForEffect(expr->left()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 812 | VisitInDuplicateContext(expr->right()); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 813 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 814 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 815 | |
| 816 | void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) { |
| 817 | bool is_logical_and = expr->op() == Token::AND; |
| 818 | Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR"); |
| 819 | Expression* left = expr->left(); |
| 820 | Expression* right = expr->right(); |
| 821 | int right_id = expr->RightId(); |
| 822 | Label done; |
| 823 | |
| 824 | if (context()->IsTest()) { |
| 825 | Label eval_right; |
| 826 | const TestContext* test = TestContext::cast(context()); |
| 827 | if (is_logical_and) { |
| 828 | VisitForControl(left, &eval_right, test->false_label(), &eval_right); |
| 829 | } else { |
| 830 | VisitForControl(left, test->true_label(), &eval_right, &eval_right); |
| 831 | } |
| 832 | PrepareForBailoutForId(right_id, NO_REGISTERS); |
| 833 | __ bind(&eval_right); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 834 | |
| 835 | } else if (context()->IsAccumulatorValue()) { |
| 836 | VisitForAccumulatorValue(left); |
| 837 | // We want the value in the accumulator for the test, and on the stack in |
| 838 | // case we need it. |
| 839 | __ push(result_register()); |
| 840 | Label discard, restore; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 841 | if (is_logical_and) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 842 | DoTest(left, &discard, &restore, &restore); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 843 | } else { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 844 | DoTest(left, &restore, &discard, &restore); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 845 | } |
| 846 | __ bind(&restore); |
| 847 | __ pop(result_register()); |
| 848 | __ jmp(&done); |
| 849 | __ bind(&discard); |
| 850 | __ Drop(1); |
| 851 | PrepareForBailoutForId(right_id, NO_REGISTERS); |
| 852 | |
| 853 | } else if (context()->IsStackValue()) { |
| 854 | VisitForAccumulatorValue(left); |
| 855 | // We want the value in the accumulator for the test, and on the stack in |
| 856 | // case we need it. |
| 857 | __ push(result_register()); |
| 858 | Label discard; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 859 | if (is_logical_and) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 860 | DoTest(left, &discard, &done, &discard); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 861 | } else { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 862 | DoTest(left, &done, &discard, &discard); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 863 | } |
| 864 | __ bind(&discard); |
| 865 | __ Drop(1); |
| 866 | PrepareForBailoutForId(right_id, NO_REGISTERS); |
| 867 | |
| 868 | } else { |
| 869 | ASSERT(context()->IsEffect()); |
| 870 | Label eval_right; |
| 871 | if (is_logical_and) { |
| 872 | VisitForControl(left, &eval_right, &done, &eval_right); |
| 873 | } else { |
| 874 | VisitForControl(left, &done, &eval_right, &eval_right); |
| 875 | } |
| 876 | PrepareForBailoutForId(right_id, NO_REGISTERS); |
| 877 | __ bind(&eval_right); |
| 878 | } |
| 879 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 880 | VisitInDuplicateContext(right); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 881 | __ bind(&done); |
| 882 | } |
| 883 | |
| 884 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 885 | void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) { |
| 886 | Token::Value op = expr->op(); |
| 887 | Comment cmnt(masm_, "[ ArithmeticExpression"); |
| 888 | Expression* left = expr->left(); |
| 889 | Expression* right = expr->right(); |
| 890 | OverwriteMode mode = |
| 891 | left->ResultOverwriteAllowed() |
| 892 | ? OVERWRITE_LEFT |
| 893 | : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE); |
| 894 | |
| 895 | VisitForStackValue(left); |
| 896 | VisitForAccumulatorValue(right); |
| 897 | |
| 898 | SetSourcePosition(expr->position()); |
| 899 | if (ShouldInlineSmiCase(op)) { |
| 900 | EmitInlineSmiBinaryOp(expr, op, mode, left, right); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 901 | } else { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 902 | EmitBinaryOp(expr, op, mode); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
| 906 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 907 | void FullCodeGenerator::VisitBlock(Block* stmt) { |
| 908 | Comment cmnt(masm_, "[ Block"); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 909 | NestedBlock nested_block(this, stmt); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 910 | SetStatementPosition(stmt); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 911 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 912 | Scope* saved_scope = scope(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 913 | // Push a block context when entering a block with block scoped variables. |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 914 | if (stmt->block_scope() != NULL) { |
| 915 | { Comment cmnt(masm_, "[ Extend block context"); |
| 916 | scope_ = stmt->block_scope(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 917 | Handle<ScopeInfo> scope_info = scope_->GetScopeInfo(); |
| 918 | int heap_slots = scope_info->ContextLength() - Context::MIN_CONTEXT_SLOTS; |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 919 | __ Push(scope_info); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 920 | PushFunctionArgumentForContextAllocation(); |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 921 | if (heap_slots <= FastNewBlockContextStub::kMaximumSlots) { |
| 922 | FastNewBlockContextStub stub(heap_slots); |
| 923 | __ CallStub(&stub); |
| 924 | } else { |
| 925 | __ CallRuntime(Runtime::kPushBlockContext, 2); |
| 926 | } |
| 927 | |
| 928 | // Replace the context stored in the frame. |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 929 | StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 930 | context_register()); |
| 931 | } |
| 932 | { Comment cmnt(masm_, "[ Declarations"); |
| 933 | VisitDeclarations(scope_->declarations()); |
| 934 | } |
| 935 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 936 | PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 937 | VisitStatements(stmt->statements()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 938 | scope_ = saved_scope; |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 939 | __ bind(nested_block.break_label()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 940 | PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 941 | |
| 942 | // Pop block context if necessary. |
| 943 | if (stmt->block_scope() != NULL) { |
| 944 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 945 | // Update local stack frame context field. |
| 946 | StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 947 | context_register()); |
| 948 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | |
| 952 | void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) { |
| 953 | Comment cmnt(masm_, "[ ExpressionStatement"); |
| 954 | SetStatementPosition(stmt); |
| 955 | VisitForEffect(stmt->expression()); |
| 956 | } |
| 957 | |
| 958 | |
| 959 | void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) { |
| 960 | Comment cmnt(masm_, "[ EmptyStatement"); |
| 961 | SetStatementPosition(stmt); |
| 962 | } |
| 963 | |
| 964 | |
| 965 | void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) { |
| 966 | Comment cmnt(masm_, "[ IfStatement"); |
| 967 | SetStatementPosition(stmt); |
| 968 | Label then_part, else_part, done; |
| 969 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 970 | if (stmt->HasElseStatement()) { |
| 971 | VisitForControl(stmt->condition(), &then_part, &else_part, &then_part); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 972 | PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 973 | __ bind(&then_part); |
| 974 | Visit(stmt->then_statement()); |
| 975 | __ jmp(&done); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 976 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 977 | PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 978 | __ bind(&else_part); |
| 979 | Visit(stmt->else_statement()); |
| 980 | } else { |
| 981 | VisitForControl(stmt->condition(), &then_part, &done, &then_part); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 982 | PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 983 | __ bind(&then_part); |
| 984 | Visit(stmt->then_statement()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 985 | |
| 986 | PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 987 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 988 | __ bind(&done); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 989 | PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | |
| 993 | void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) { |
| 994 | Comment cmnt(masm_, "[ ContinueStatement"); |
| 995 | SetStatementPosition(stmt); |
| 996 | NestedStatement* current = nesting_stack_; |
| 997 | int stack_depth = 0; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 998 | int context_length = 0; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 999 | // When continuing, we clobber the unpredictable value in the accumulator |
| 1000 | // with one that's safe for GC. If we hit an exit from the try block of |
| 1001 | // try...finally on our way out, we will unconditionally preserve the |
| 1002 | // accumulator on the stack. |
| 1003 | ClearAccumulator(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1004 | while (!current->IsContinueTarget(stmt->target())) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1005 | current = current->Exit(&stack_depth, &context_length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1006 | } |
| 1007 | __ Drop(stack_depth); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1008 | if (context_length > 0) { |
| 1009 | while (context_length > 0) { |
| 1010 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 1011 | --context_length; |
| 1012 | } |
| 1013 | StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 1014 | context_register()); |
| 1015 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1016 | |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1017 | __ jmp(current->AsIteration()->continue_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | |
| 1021 | void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) { |
| 1022 | Comment cmnt(masm_, "[ BreakStatement"); |
| 1023 | SetStatementPosition(stmt); |
| 1024 | NestedStatement* current = nesting_stack_; |
| 1025 | int stack_depth = 0; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1026 | int context_length = 0; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1027 | // When breaking, we clobber the unpredictable value in the accumulator |
| 1028 | // with one that's safe for GC. If we hit an exit from the try block of |
| 1029 | // try...finally on our way out, we will unconditionally preserve the |
| 1030 | // accumulator on the stack. |
| 1031 | ClearAccumulator(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1032 | while (!current->IsBreakTarget(stmt->target())) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1033 | current = current->Exit(&stack_depth, &context_length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1034 | } |
| 1035 | __ Drop(stack_depth); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1036 | if (context_length > 0) { |
| 1037 | while (context_length > 0) { |
| 1038 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 1039 | --context_length; |
| 1040 | } |
| 1041 | StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 1042 | context_register()); |
| 1043 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1044 | |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1045 | __ jmp(current->AsBreakable()->break_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | |
| 1049 | void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) { |
| 1050 | Comment cmnt(masm_, "[ ReturnStatement"); |
| 1051 | SetStatementPosition(stmt); |
| 1052 | Expression* expr = stmt->expression(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1053 | VisitForAccumulatorValue(expr); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1054 | |
| 1055 | // Exit all nested statements. |
| 1056 | NestedStatement* current = nesting_stack_; |
| 1057 | int stack_depth = 0; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1058 | int context_length = 0; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1059 | while (current != NULL) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1060 | current = current->Exit(&stack_depth, &context_length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1061 | } |
| 1062 | __ Drop(stack_depth); |
| 1063 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1064 | EmitReturnSequence(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1068 | void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) { |
| 1069 | Comment cmnt(masm_, "[ WithStatement"); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1070 | SetStatementPosition(stmt); |
| 1071 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1072 | VisitForStackValue(stmt->expression()); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 1073 | PushFunctionArgumentForContextAllocation(); |
| 1074 | __ CallRuntime(Runtime::kPushWithContext, 2); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1075 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1076 | |
| 1077 | { WithOrCatch body(this); |
| 1078 | Visit(stmt->statement()); |
| 1079 | } |
| 1080 | |
| 1081 | // Pop context. |
| 1082 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 1083 | // Update local stack frame context field. |
| 1084 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1088 | void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 1089 | Comment cmnt(masm_, "[ DoWhileStatement"); |
| 1090 | SetStatementPosition(stmt); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1091 | Label body, stack_check; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1092 | |
| 1093 | Iteration loop_statement(this, stmt); |
| 1094 | increment_loop_depth(); |
| 1095 | |
| 1096 | __ bind(&body); |
| 1097 | Visit(stmt->body()); |
| 1098 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1099 | // Record the position of the do while condition and make sure it is |
| 1100 | // possible to break on the condition. |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1101 | __ bind(loop_statement.continue_label()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1102 | PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1103 | SetExpressionPosition(stmt->cond(), stmt->condition_position()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1104 | VisitForControl(stmt->cond(), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1105 | &stack_check, |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1106 | loop_statement.break_label(), |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1107 | &stack_check); |
| 1108 | |
| 1109 | // Check stack before looping. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1110 | PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1111 | __ bind(&stack_check); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 1112 | EmitStackCheck(stmt, &body); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1113 | __ jmp(&body); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1114 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1115 | PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1116 | __ bind(loop_statement.break_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1117 | decrement_loop_depth(); |
| 1118 | } |
| 1119 | |
| 1120 | |
| 1121 | void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) { |
| 1122 | Comment cmnt(masm_, "[ WhileStatement"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1123 | Label test, body; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1124 | |
| 1125 | Iteration loop_statement(this, stmt); |
| 1126 | increment_loop_depth(); |
| 1127 | |
| 1128 | // Emit the test at the bottom of the loop. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1129 | __ jmp(&test); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1130 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1131 | PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1132 | __ bind(&body); |
| 1133 | Visit(stmt->body()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1134 | |
| 1135 | // Emit the statement position here as this is where the while |
| 1136 | // statement code starts. |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1137 | __ bind(loop_statement.continue_label()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1138 | SetStatementPosition(stmt); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1139 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1140 | // Check stack before looping. |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 1141 | EmitStackCheck(stmt, &body); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1142 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1143 | __ bind(&test); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1144 | VisitForControl(stmt->cond(), |
| 1145 | &body, |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1146 | loop_statement.break_label(), |
| 1147 | loop_statement.break_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1148 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1149 | PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1150 | __ bind(loop_statement.break_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1151 | decrement_loop_depth(); |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | void FullCodeGenerator::VisitForStatement(ForStatement* stmt) { |
| 1156 | Comment cmnt(masm_, "[ ForStatement"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1157 | Label test, body; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1158 | |
| 1159 | Iteration loop_statement(this, stmt); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1160 | |
| 1161 | // Set statement position for a break slot before entering the for-body. |
| 1162 | SetStatementPosition(stmt); |
| 1163 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1164 | if (stmt->init() != NULL) { |
| 1165 | Visit(stmt->init()); |
| 1166 | } |
| 1167 | |
| 1168 | increment_loop_depth(); |
| 1169 | // Emit the test at the bottom of the loop (even if empty). |
| 1170 | __ jmp(&test); |
| 1171 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1172 | PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1173 | __ bind(&body); |
| 1174 | Visit(stmt->body()); |
| 1175 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1176 | PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1177 | __ bind(loop_statement.continue_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1178 | if (stmt->next() != NULL) { |
| 1179 | Visit(stmt->next()); |
| 1180 | } |
| 1181 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1182 | // Emit the statement position here as this is where the for |
| 1183 | // statement code starts. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1184 | SetStatementPosition(stmt); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1185 | |
| 1186 | // Check stack before looping. |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 1187 | EmitStackCheck(stmt, &body); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1188 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1189 | __ bind(&test); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1190 | if (stmt->cond() != NULL) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1191 | VisitForControl(stmt->cond(), |
| 1192 | &body, |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1193 | loop_statement.break_label(), |
| 1194 | loop_statement.break_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1195 | } else { |
| 1196 | __ jmp(&body); |
| 1197 | } |
| 1198 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1199 | PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1200 | __ bind(loop_statement.break_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1201 | decrement_loop_depth(); |
| 1202 | } |
| 1203 | |
| 1204 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1205 | void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 1206 | Comment cmnt(masm_, "[ TryCatchStatement"); |
| 1207 | SetStatementPosition(stmt); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1208 | // The try block adds a handler to the exception handler chain before |
| 1209 | // entering, and removes it again when exiting normally. If an exception |
| 1210 | // is thrown during execution of the try block, the handler is consumed |
| 1211 | // and control is passed to the catch block with the exception in the |
| 1212 | // result register. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1213 | |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1214 | Label try_entry, handler_entry, exit; |
| 1215 | __ jmp(&try_entry); |
| 1216 | __ bind(&handler_entry); |
| 1217 | handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos())); |
| 1218 | // Exception handler code, the exception is in the result register. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1219 | // Extend the context before executing the catch block. |
| 1220 | { Comment cmnt(masm_, "[ Extend catch context"); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1221 | __ Push(stmt->variable()->name()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1222 | __ push(result_register()); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 1223 | PushFunctionArgumentForContextAllocation(); |
| 1224 | __ CallRuntime(Runtime::kPushCatchContext, 3); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1225 | StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 1226 | context_register()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1229 | Scope* saved_scope = scope(); |
| 1230 | scope_ = stmt->scope(); |
| 1231 | ASSERT(scope_->declarations()->is_empty()); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1232 | { WithOrCatch catch_body(this); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1233 | Visit(stmt->catch_block()); |
| 1234 | } |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1235 | // Restore the context. |
| 1236 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 1237 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1238 | scope_ = saved_scope; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1239 | __ jmp(&exit); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1240 | |
| 1241 | // Try block code. Sets up the exception handler chain. |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1242 | __ bind(&try_entry); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1243 | __ PushTryHandler(StackHandler::CATCH, stmt->index()); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1244 | { TryCatch try_body(this); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1245 | Visit(stmt->try_block()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1246 | } |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1247 | __ PopTryHandler(); |
| 1248 | __ bind(&exit); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | |
| 1252 | void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
| 1253 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
| 1254 | SetStatementPosition(stmt); |
| 1255 | // Try finally is compiled by setting up a try-handler on the stack while |
| 1256 | // executing the try body, and removing it again afterwards. |
| 1257 | // |
| 1258 | // The try-finally construct can enter the finally block in three ways: |
| 1259 | // 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] | 1260 | // calls the finally block code before continuing. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1261 | // 2. By exiting the try-block with a function-local control flow transfer |
| 1262 | // (break/continue/return). The site of the, e.g., break removes the |
| 1263 | // try handler and calls the finally block code before continuing |
| 1264 | // its outward control transfer. |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1265 | // 3. By exiting the try-block with a thrown exception. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1266 | // This can happen in nested function calls. It traverses the try-handler |
| 1267 | // chain and consumes the try-handler entry before jumping to the |
| 1268 | // handler code. The handler code then calls the finally-block before |
| 1269 | // rethrowing the exception. |
| 1270 | // |
| 1271 | // The finally block must assume a return address on top of the stack |
| 1272 | // (or in the link register on ARM chips) and a value (return value or |
| 1273 | // exception) in the result register (rax/eax/r0), both of which must |
| 1274 | // be preserved. The return address isn't GC-safe, so it should be |
| 1275 | // cooked before GC. |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1276 | Label try_entry, handler_entry, finally_entry; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1277 | |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1278 | // Jump to try-handler setup and try-block code. |
| 1279 | __ jmp(&try_entry); |
| 1280 | __ bind(&handler_entry); |
| 1281 | handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos())); |
| 1282 | // Exception handler code. This code is only executed when an exception |
| 1283 | // is thrown. The exception is in the result register, and must be |
| 1284 | // preserved by the finally block. Call the finally block and then |
| 1285 | // rethrow the exception if it returns. |
| 1286 | __ Call(&finally_entry); |
| 1287 | __ push(result_register()); |
| 1288 | __ CallRuntime(Runtime::kReThrow, 1); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1289 | |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1290 | // Finally block implementation. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1291 | __ bind(&finally_entry); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1292 | EnterFinallyBlock(); |
| 1293 | { Finally finally_body(this); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1294 | Visit(stmt->finally_block()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1295 | } |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1296 | ExitFinallyBlock(); // Return to the calling code. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1297 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1298 | // Set up try handler. |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1299 | __ bind(&try_entry); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1300 | __ PushTryHandler(StackHandler::FINALLY, stmt->index()); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1301 | { TryFinally try_body(this, &finally_entry); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1302 | Visit(stmt->try_block()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1303 | } |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1304 | __ PopTryHandler(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1305 | // Execute the finally block on the way out. Clobber the unpredictable |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1306 | // value in the result register with one that's safe for GC because the |
| 1307 | // finally block will unconditionally preserve the result register on the |
| 1308 | // stack. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1309 | ClearAccumulator(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1310 | __ Call(&finally_entry); |
| 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) { |
| 1315 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1316 | Comment cmnt(masm_, "[ DebuggerStatement"); |
| 1317 | SetStatementPosition(stmt); |
| 1318 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1319 | __ DebugBreak(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1320 | // Ignore the return value. |
| 1321 | #endif |
| 1322 | } |
| 1323 | |
| 1324 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1325 | void FullCodeGenerator::VisitConditional(Conditional* expr) { |
| 1326 | Comment cmnt(masm_, "[ Conditional"); |
| 1327 | Label true_case, false_case, done; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1328 | VisitForControl(expr->condition(), &true_case, &false_case, &true_case); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1329 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1330 | PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1331 | __ bind(&true_case); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1332 | SetExpressionPosition(expr->then_expression(), |
| 1333 | expr->then_expression_position()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1334 | if (context()->IsTest()) { |
| 1335 | const TestContext* for_test = TestContext::cast(context()); |
| 1336 | VisitForControl(expr->then_expression(), |
| 1337 | for_test->true_label(), |
| 1338 | for_test->false_label(), |
| 1339 | NULL); |
| 1340 | } else { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1341 | VisitInDuplicateContext(expr->then_expression()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1342 | __ jmp(&done); |
| 1343 | } |
| 1344 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1345 | PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1346 | __ bind(&false_case); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1347 | SetExpressionPosition(expr->else_expression(), |
| 1348 | expr->else_expression_position()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1349 | VisitInDuplicateContext(expr->else_expression()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1350 | // 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] | 1351 | if (!context()->IsTest()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1352 | __ bind(&done); |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1357 | void FullCodeGenerator::VisitLiteral(Literal* expr) { |
| 1358 | Comment cmnt(masm_, "[ Literal"); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1359 | context()->Plug(expr->handle()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1363 | void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 1364 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 1365 | |
| 1366 | // Build the function boilerplate and instantiate it. |
| 1367 | Handle<SharedFunctionInfo> function_info = |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1368 | Compiler::BuildFunctionInfo(expr, script()); |
| 1369 | if (function_info.is_null()) { |
| 1370 | SetStackOverflow(); |
| 1371 | return; |
| 1372 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1373 | EmitNewClosure(function_info, expr->pretenure()); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | |
| 1377 | void FullCodeGenerator::VisitSharedFunctionInfoLiteral( |
| 1378 | SharedFunctionInfoLiteral* expr) { |
| 1379 | Comment cmnt(masm_, "[ SharedFunctionInfoLiteral"); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1380 | EmitNewClosure(expr->shared_function_info(), false); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1384 | void FullCodeGenerator::VisitThrow(Throw* expr) { |
| 1385 | Comment cmnt(masm_, "[ Throw"); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1386 | VisitForStackValue(expr->exception()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1387 | __ CallRuntime(Runtime::kThrow, 1); |
| 1388 | // Never returns here. |
| 1389 | } |
| 1390 | |
| 1391 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1392 | FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit( |
| 1393 | int* stack_depth, |
| 1394 | int* context_length) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1395 | // The macros used here must preserve the result register. |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1396 | __ Drop(*stack_depth); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1397 | __ PopTryHandler(); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1398 | *stack_depth = 0; |
| 1399 | return previous_; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1402 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1403 | bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1404 | Expression* sub_expr; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1405 | Handle<String> check; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1406 | if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1407 | EmitLiteralCompareTypeof(expr, sub_expr, check); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1408 | return true; |
| 1409 | } |
| 1410 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1411 | if (expr->IsLiteralCompareUndefined(&sub_expr)) { |
| 1412 | EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue); |
| 1413 | return true; |
| 1414 | } |
| 1415 | |
| 1416 | if (expr->IsLiteralCompareNull(&sub_expr)) { |
| 1417 | EmitLiteralCompareNil(expr, sub_expr, kNullValue); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1418 | return true; |
| 1419 | } |
| 1420 | |
| 1421 | return false; |
| 1422 | } |
| 1423 | |
| 1424 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1425 | #undef __ |
| 1426 | |
| 1427 | |
| 1428 | } } // namespace v8::internal |