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