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" |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 39 | #include "snapshot.h" |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 40 | #include "stub-cache.h" |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 41 | |
| 42 | namespace v8 { |
| 43 | namespace internal { |
| 44 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 45 | void BreakableStatementChecker::Check(Statement* stmt) { |
| 46 | Visit(stmt); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | void BreakableStatementChecker::Check(Expression* expr) { |
| 51 | Visit(expr); |
| 52 | } |
| 53 | |
| 54 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 55 | void BreakableStatementChecker::VisitVariableDeclaration( |
| 56 | VariableDeclaration* decl) { |
| 57 | } |
| 58 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 59 | void BreakableStatementChecker::VisitFunctionDeclaration( |
| 60 | FunctionDeclaration* decl) { |
| 61 | } |
| 62 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 63 | void BreakableStatementChecker::VisitModuleDeclaration( |
| 64 | ModuleDeclaration* decl) { |
| 65 | } |
| 66 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 67 | void BreakableStatementChecker::VisitImportDeclaration( |
| 68 | ImportDeclaration* decl) { |
| 69 | } |
| 70 | |
| 71 | void BreakableStatementChecker::VisitExportDeclaration( |
| 72 | ExportDeclaration* decl) { |
| 73 | } |
| 74 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 75 | |
| 76 | void BreakableStatementChecker::VisitModuleLiteral(ModuleLiteral* module) { |
| 77 | } |
| 78 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 79 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 80 | void BreakableStatementChecker::VisitModuleVariable(ModuleVariable* module) { |
| 81 | } |
| 82 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 83 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 84 | void BreakableStatementChecker::VisitModulePath(ModulePath* module) { |
| 85 | } |
| 86 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 87 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 88 | void BreakableStatementChecker::VisitModuleUrl(ModuleUrl* module) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 92 | void BreakableStatementChecker::VisitModuleStatement(ModuleStatement* stmt) { |
| 93 | } |
| 94 | |
| 95 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 96 | void BreakableStatementChecker::VisitBlock(Block* stmt) { |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void BreakableStatementChecker::VisitExpressionStatement( |
| 101 | ExpressionStatement* stmt) { |
| 102 | // Check if expression is breakable. |
| 103 | Visit(stmt->expression()); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) { |
| 108 | } |
| 109 | |
| 110 | |
| 111 | void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) { |
| 112 | // If the condition is breakable the if statement is breakable. |
| 113 | Visit(stmt->condition()); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | void BreakableStatementChecker::VisitContinueStatement( |
| 118 | ContinueStatement* stmt) { |
| 119 | } |
| 120 | |
| 121 | |
| 122 | void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) { |
| 123 | } |
| 124 | |
| 125 | |
| 126 | void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) { |
| 127 | // Return is breakable if the expression is. |
| 128 | Visit(stmt->expression()); |
| 129 | } |
| 130 | |
| 131 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 132 | void BreakableStatementChecker::VisitWithStatement(WithStatement* stmt) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 133 | Visit(stmt->expression()); |
| 134 | } |
| 135 | |
| 136 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 137 | void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) { |
| 138 | // Switch statements breakable if the tag expression is. |
| 139 | Visit(stmt->tag()); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 144 | // Mark do while as breakable to avoid adding a break slot in front of it. |
| 145 | is_breakable_ = true; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) { |
| 150 | // Mark while statements breakable if the condition expression is. |
| 151 | Visit(stmt->cond()); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) { |
| 156 | // Mark for statements breakable if the condition expression is. |
| 157 | if (stmt->cond() != NULL) { |
| 158 | Visit(stmt->cond()); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
| 163 | void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) { |
| 164 | // Mark for in statements breakable if the enumerable expression is. |
| 165 | Visit(stmt->enumerable()); |
| 166 | } |
| 167 | |
| 168 | |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 169 | void BreakableStatementChecker::VisitForOfStatement(ForOfStatement* stmt) { |
| 170 | // For-of is breakable because of the next() call. |
| 171 | is_breakable_ = true; |
| 172 | } |
| 173 | |
| 174 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 175 | void BreakableStatementChecker::VisitTryCatchStatement( |
| 176 | TryCatchStatement* stmt) { |
| 177 | // Mark try catch as breakable to avoid adding a break slot in front of it. |
| 178 | is_breakable_ = true; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | void BreakableStatementChecker::VisitTryFinallyStatement( |
| 183 | TryFinallyStatement* stmt) { |
| 184 | // Mark try finally as breakable to avoid adding a break slot in front of it. |
| 185 | is_breakable_ = true; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | void BreakableStatementChecker::VisitDebuggerStatement( |
| 190 | DebuggerStatement* stmt) { |
| 191 | // The debugger statement is breakable. |
| 192 | is_breakable_ = true; |
| 193 | } |
| 194 | |
| 195 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 196 | void BreakableStatementChecker::VisitCaseClause(CaseClause* clause) { |
| 197 | } |
| 198 | |
| 199 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 200 | void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 201 | } |
| 202 | |
| 203 | |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 204 | void BreakableStatementChecker::VisitNativeFunctionLiteral( |
| 205 | NativeFunctionLiteral* expr) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | |
| 209 | void BreakableStatementChecker::VisitConditional(Conditional* expr) { |
| 210 | } |
| 211 | |
| 212 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 213 | void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) { |
| 214 | } |
| 215 | |
| 216 | |
| 217 | void BreakableStatementChecker::VisitLiteral(Literal* expr) { |
| 218 | } |
| 219 | |
| 220 | |
| 221 | void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 222 | } |
| 223 | |
| 224 | |
| 225 | void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) { |
| 226 | } |
| 227 | |
| 228 | |
| 229 | void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) { |
| 230 | } |
| 231 | |
| 232 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 233 | void BreakableStatementChecker::VisitAssignment(Assignment* expr) { |
| 234 | // If assigning to a property (including a global property) the assignment is |
| 235 | // breakable. |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 236 | VariableProxy* proxy = expr->target()->AsVariableProxy(); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 237 | Property* prop = expr->target()->AsProperty(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 238 | if (prop != NULL || (proxy != NULL && proxy->var()->IsUnallocated())) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 239 | is_breakable_ = true; |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | // Otherwise the assignment is breakable if the assigned value is. |
| 244 | Visit(expr->value()); |
| 245 | } |
| 246 | |
| 247 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 248 | void BreakableStatementChecker::VisitYield(Yield* expr) { |
| 249 | // Yield is breakable if the expression is. |
| 250 | Visit(expr->expression()); |
| 251 | } |
| 252 | |
| 253 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 254 | void BreakableStatementChecker::VisitThrow(Throw* expr) { |
| 255 | // Throw is breakable if the expression is. |
| 256 | Visit(expr->exception()); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | void BreakableStatementChecker::VisitProperty(Property* expr) { |
| 261 | // Property load is breakable. |
| 262 | is_breakable_ = true; |
| 263 | } |
| 264 | |
| 265 | |
| 266 | void BreakableStatementChecker::VisitCall(Call* expr) { |
| 267 | // Function calls both through IC and call stub are breakable. |
| 268 | is_breakable_ = true; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | void BreakableStatementChecker::VisitCallNew(CallNew* expr) { |
| 273 | // Function calls through new are breakable. |
| 274 | is_breakable_ = true; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) { |
| 279 | } |
| 280 | |
| 281 | |
| 282 | void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) { |
| 283 | Visit(expr->expression()); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) { |
| 288 | Visit(expr->expression()); |
| 289 | } |
| 290 | |
| 291 | |
| 292 | void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) { |
| 293 | Visit(expr->left()); |
jkummerow@chromium.org | ddda9e8 | 2011-07-06 11:27:02 +0000 | [diff] [blame] | 294 | if (expr->op() != Token::AND && |
| 295 | expr->op() != Token::OR) { |
| 296 | Visit(expr->right()); |
| 297 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | |
| 301 | void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) { |
| 302 | Visit(expr->left()); |
| 303 | Visit(expr->right()); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) { |
| 308 | } |
| 309 | |
| 310 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 311 | #define __ ACCESS_MASM(masm()) |
| 312 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 313 | bool FullCodeGenerator::MakeCode(CompilationInfo* info) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 314 | Isolate* isolate = info->isolate(); |
yangguo@chromium.org | 4954674 | 2013-12-23 16:17:49 +0000 | [diff] [blame] | 315 | |
| 316 | Logger::TimerEventScope timer( |
| 317 | isolate, Logger::TimerEventScope::v8_compile_full_code); |
| 318 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 319 | Handle<Script> script = info->script(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 320 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 321 | int len = String::cast(script->source())->length(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 322 | isolate->counters()->total_full_codegen_source_size()->Increment(len); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 323 | } |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 324 | CodeGenerator::MakeCodePrologue(info, "full"); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 325 | const int kInitialBufferSize = 4 * KB; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 326 | MacroAssembler masm(info->isolate(), NULL, kInitialBufferSize); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 327 | #ifdef ENABLE_GDB_JIT_INTERFACE |
| 328 | masm.positions_recorder()->StartGDBJITLineInfoRecording(); |
| 329 | #endif |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 330 | LOG_CODE_EVENT(isolate, |
| 331 | CodeStartLinePosInfoRecordEvent(masm.positions_recorder())); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 332 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 333 | FullCodeGenerator cgen(&masm, info); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 334 | cgen.Generate(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 335 | if (cgen.HasStackOverflow()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 336 | ASSERT(!isolate->has_pending_exception()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 337 | return false; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 338 | } |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 339 | unsigned table_offset = cgen.EmitBackEdgeTable(); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 340 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 341 | Code::Flags flags = Code::ComputeFlags(Code::FUNCTION); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 342 | Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 343 | code->set_optimizable(info->IsOptimizable() && |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 344 | !info->function()->dont_optimize() && |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 345 | info->function()->scope()->AllowsLazyCompilation()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 346 | cgen.PopulateDeoptimizationData(code); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 347 | cgen.PopulateTypeFeedbackInfo(code); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 348 | cgen.PopulateTypeFeedbackCells(code); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 349 | code->set_has_deoptimization_support(info->HasDeoptimizationSupport()); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 350 | code->set_handler_table(*cgen.handler_table()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 351 | #ifdef ENABLE_DEBUGGER_SUPPORT |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 352 | code->set_compiled_optimizable(info->IsOptimizable()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 353 | #endif // ENABLE_DEBUGGER_SUPPORT |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 354 | code->set_allow_osr_at_loop_nesting_level(0); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 355 | code->set_profiler_ticks(0); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 356 | code->set_back_edge_table_offset(table_offset); |
| 357 | code->set_back_edges_patched_for_osr(false); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 358 | CodeGenerator::PrintCode(code, info); |
rossberg@chromium.org | ebeba02 | 2013-08-19 09:36:44 +0000 | [diff] [blame] | 359 | info->SetCode(code); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 360 | #ifdef ENABLE_GDB_JIT_INTERFACE |
rossberg@chromium.org | ebeba02 | 2013-08-19 09:36:44 +0000 | [diff] [blame] | 361 | if (FLAG_gdbjit) { |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 362 | GDBJITLineInfo* lineinfo = |
| 363 | masm.positions_recorder()->DetachGDBJITLineInfo(); |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 364 | GDBJIT(RegisterDetailedLineInfo(*code, lineinfo)); |
| 365 | } |
| 366 | #endif |
rossberg@chromium.org | ebeba02 | 2013-08-19 09:36:44 +0000 | [diff] [blame] | 367 | void* line_info = masm.positions_recorder()->DetachJITHandlerData(); |
| 368 | LOG_CODE_EVENT(isolate, CodeEndLinePosInfoRecordEvent(*code, line_info)); |
| 369 | return true; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 373 | unsigned FullCodeGenerator::EmitBackEdgeTable() { |
| 374 | // The back edge table consists of a length (in number of entries) |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 375 | // field, and then a sequence of entries. Each entry is a pair of AST id |
| 376 | // and code-relative pc offset. |
| 377 | masm()->Align(kIntSize); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 378 | unsigned offset = masm()->pc_offset(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 379 | unsigned length = back_edges_.length(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 380 | __ dd(length); |
| 381 | for (unsigned i = 0; i < length; ++i) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 382 | __ dd(back_edges_[i].id.ToInt()); |
| 383 | __ dd(back_edges_[i].pc); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 384 | __ dd(back_edges_[i].loop_depth); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 385 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 386 | return offset; |
| 387 | } |
| 388 | |
| 389 | |
| 390 | void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) { |
| 391 | // Fill in the deoptimization information. |
| 392 | ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty()); |
| 393 | if (!info_->HasDeoptimizationSupport()) return; |
| 394 | int length = bailout_entries_.length(); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 395 | Handle<DeoptimizationOutputData> data = isolate()->factory()-> |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 396 | NewDeoptimizationOutputData(length, TENURED); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 397 | for (int i = 0; i < length; i++) { |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 398 | data->SetAstId(i, bailout_entries_[i].id); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 399 | data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state)); |
| 400 | } |
| 401 | code->set_deoptimization_data(*data); |
| 402 | } |
| 403 | |
| 404 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 405 | void FullCodeGenerator::PopulateTypeFeedbackInfo(Handle<Code> code) { |
| 406 | Handle<TypeFeedbackInfo> info = isolate()->factory()->NewTypeFeedbackInfo(); |
| 407 | info->set_ic_total_count(ic_total_count_); |
yangguo@chromium.org | a7d3df9 | 2012-02-27 11:46:55 +0000 | [diff] [blame] | 408 | ASSERT(!isolate()->heap()->InNewSpace(*info)); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 409 | code->set_type_feedback_info(*info); |
| 410 | } |
| 411 | |
| 412 | |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 413 | void FullCodeGenerator::Initialize() { |
| 414 | // The generation of debug code must match between the snapshot code and the |
| 415 | // code that is generated later. This is assumed by the debugger when it is |
| 416 | // calculating PC offsets after generating a debug version of code. Therefore |
| 417 | // we disable the production of debug code in the full compiler if we are |
| 418 | // either generating a snapshot or we booted from a snapshot. |
| 419 | generate_debug_code_ = FLAG_debug_code && |
| 420 | !Serializer::enabled() && |
| 421 | !Snapshot::HaveASnapshotToStartFrom(); |
| 422 | masm_->set_emit_debug_code(generate_debug_code_); |
| 423 | masm_->set_predictable_code_size(true); |
machenbach@chromium.org | 6d26cbb | 2014-01-22 10:50:56 +0000 | [diff] [blame] | 424 | InitializeAstVisitor(info_->zone()); |
jkummerow@chromium.org | 000f7fb | 2012-08-01 11:14:42 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 428 | void FullCodeGenerator::PopulateTypeFeedbackCells(Handle<Code> code) { |
| 429 | if (type_feedback_cells_.is_empty()) return; |
| 430 | int length = type_feedback_cells_.length(); |
| 431 | int array_size = TypeFeedbackCells::LengthOfFixedArray(length); |
| 432 | Handle<TypeFeedbackCells> cache = Handle<TypeFeedbackCells>::cast( |
| 433 | isolate()->factory()->NewFixedArray(array_size, TENURED)); |
| 434 | for (int i = 0; i < length; i++) { |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 435 | cache->SetAstId(i, type_feedback_cells_[i].ast_id); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 436 | cache->SetCell(i, *type_feedback_cells_[i].cell); |
| 437 | } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 438 | TypeFeedbackInfo::cast(code->type_feedback_info())->set_type_feedback_cells( |
| 439 | *cache); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 443 | void FullCodeGenerator::PrepareForBailout(Expression* node, State state) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 444 | PrepareForBailoutForId(node->id(), state); |
| 445 | } |
| 446 | |
| 447 | |
ulan@chromium.org | 9cbaabd | 2014-01-08 10:55:36 +0000 | [diff] [blame] | 448 | void FullCodeGenerator::CallLoadIC(ContextualMode mode, TypeFeedbackId id) { |
| 449 | Handle<Code> ic = LoadIC::initialize_stub(isolate(), mode); |
| 450 | CallIC(ic, mode, id); |
| 451 | } |
| 452 | |
| 453 | |
| 454 | void FullCodeGenerator::CallStoreIC(ContextualMode mode, TypeFeedbackId id) { |
machenbach@chromium.org | 43c51e5 | 2014-01-20 07:57:28 +0000 | [diff] [blame] | 455 | Handle<Code> ic = StoreIC::initialize_stub(isolate(), strict_mode()); |
ulan@chromium.org | 9cbaabd | 2014-01-08 10:55:36 +0000 | [diff] [blame] | 456 | CallIC(ic, mode, id); |
| 457 | } |
| 458 | |
| 459 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 460 | void FullCodeGenerator::RecordJSReturnSite(Call* call) { |
| 461 | // We record the offset of the function return so we can rebuild the frame |
| 462 | // if the function was inlined, i.e., this is the return address in the |
| 463 | // inlined function's frame. |
| 464 | // |
| 465 | // The state is ignored. We defensively set it to TOS_REG, which is the |
| 466 | // real state of the unoptimized code at the return site. |
| 467 | PrepareForBailoutForId(call->ReturnId(), TOS_REG); |
| 468 | #ifdef DEBUG |
| 469 | // In debug builds, mark the return so we can verify that this function |
| 470 | // was called. |
| 471 | ASSERT(!call->return_is_recorded_); |
| 472 | call->return_is_recorded_ = true; |
| 473 | #endif |
| 474 | } |
| 475 | |
| 476 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 477 | void FullCodeGenerator::PrepareForBailoutForId(BailoutId id, State state) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 478 | // There's no need to prepare this code for bailouts from already optimized |
| 479 | // code or code that can't be optimized. |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 480 | if (!info_->HasDeoptimizationSupport()) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 481 | unsigned pc_and_state = |
| 482 | StateField::encode(state) | PcField::encode(masm_->pc_offset()); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 483 | ASSERT(Smi::IsValid(pc_and_state)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 484 | BailoutEntry entry = { id, pc_and_state }; |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 485 | ASSERT(!prepared_bailout_ids_.Contains(id.ToInt())); |
| 486 | prepared_bailout_ids_.Add(id.ToInt(), zone()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 487 | bailout_entries_.Add(entry, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 491 | void FullCodeGenerator::RecordTypeFeedbackCell( |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 492 | TypeFeedbackId id, Handle<Cell> cell) { |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 493 | TypeFeedbackCellEntry entry = { id, cell }; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 494 | type_feedback_cells_.Add(entry, zone()); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | |
rossberg@chromium.org | cddc71f | 2012-12-07 12:40:13 +0000 | [diff] [blame] | 498 | void FullCodeGenerator::RecordBackEdge(BailoutId ast_id) { |
| 499 | // The pc offset does not need to be encoded and packed together with a state. |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 500 | ASSERT(masm_->pc_offset() > 0); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 501 | ASSERT(loop_depth() > 0); |
| 502 | uint8_t depth = Min(loop_depth(), Code::kMaxLoopNestingMarker); |
| 503 | BackEdgeEntry entry = |
| 504 | { ast_id, static_cast<unsigned>(masm_->pc_offset()), depth }; |
| 505 | back_edges_.Add(entry, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 509 | bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 510 | // Inline smi case inside loops, but not division and modulo which |
| 511 | // are too complicated and take up too much space. |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 512 | if (op == Token::DIV ||op == Token::MOD) return false; |
| 513 | if (FLAG_always_inline_smi_code) return true; |
| 514 | return loop_depth_ > 0; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 518 | void FullCodeGenerator::EffectContext::Plug(Register reg) const { |
| 519 | } |
| 520 | |
| 521 | |
| 522 | void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 523 | __ Move(result_register(), reg); |
| 524 | } |
| 525 | |
| 526 | |
| 527 | void FullCodeGenerator::StackValueContext::Plug(Register reg) const { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 528 | __ Push(reg); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
| 532 | void FullCodeGenerator::TestContext::Plug(Register reg) const { |
| 533 | // For simplicity we always test the accumulator register. |
| 534 | __ Move(result_register(), reg); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 535 | codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 536 | codegen()->DoTest(this); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | |
| 540 | void FullCodeGenerator::EffectContext::PlugTOS() const { |
| 541 | __ Drop(1); |
| 542 | } |
| 543 | |
| 544 | |
| 545 | void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const { |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 546 | __ Pop(result_register()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | |
| 550 | void FullCodeGenerator::StackValueContext::PlugTOS() const { |
| 551 | } |
| 552 | |
| 553 | |
| 554 | void FullCodeGenerator::TestContext::PlugTOS() const { |
| 555 | // For simplicity we always test the accumulator register. |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 556 | __ Pop(result_register()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 557 | codegen()->PrepareForBailoutBeforeSplit(condition(), false, NULL, NULL); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 558 | codegen()->DoTest(this); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | |
| 562 | void FullCodeGenerator::EffectContext::PrepareTest( |
| 563 | Label* materialize_true, |
| 564 | Label* materialize_false, |
| 565 | Label** if_true, |
| 566 | Label** if_false, |
| 567 | Label** fall_through) const { |
| 568 | // In an effect context, the true and the false case branch to the |
| 569 | // same label. |
| 570 | *if_true = *if_false = *fall_through = materialize_true; |
| 571 | } |
| 572 | |
| 573 | |
| 574 | void FullCodeGenerator::AccumulatorValueContext::PrepareTest( |
| 575 | Label* materialize_true, |
| 576 | Label* materialize_false, |
| 577 | Label** if_true, |
| 578 | Label** if_false, |
| 579 | Label** fall_through) const { |
| 580 | *if_true = *fall_through = materialize_true; |
| 581 | *if_false = materialize_false; |
| 582 | } |
| 583 | |
| 584 | |
| 585 | void FullCodeGenerator::StackValueContext::PrepareTest( |
| 586 | Label* materialize_true, |
| 587 | Label* materialize_false, |
| 588 | Label** if_true, |
| 589 | Label** if_false, |
| 590 | Label** fall_through) const { |
| 591 | *if_true = *fall_through = materialize_true; |
| 592 | *if_false = materialize_false; |
| 593 | } |
| 594 | |
| 595 | |
| 596 | void FullCodeGenerator::TestContext::PrepareTest( |
| 597 | Label* materialize_true, |
| 598 | Label* materialize_false, |
| 599 | Label** if_true, |
| 600 | Label** if_false, |
| 601 | Label** fall_through) const { |
| 602 | *if_true = true_label_; |
| 603 | *if_false = false_label_; |
| 604 | *fall_through = fall_through_; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 608 | void FullCodeGenerator::DoTest(const TestContext* context) { |
| 609 | DoTest(context->condition(), |
| 610 | context->true_label(), |
| 611 | context->false_label(), |
| 612 | context->fall_through()); |
| 613 | } |
| 614 | |
| 615 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 616 | void FullCodeGenerator::AllocateModules(ZoneList<Declaration*>* declarations) { |
| 617 | ASSERT(scope_->is_global_scope()); |
| 618 | |
| 619 | for (int i = 0; i < declarations->length(); i++) { |
| 620 | ModuleDeclaration* declaration = declarations->at(i)->AsModuleDeclaration(); |
| 621 | if (declaration != NULL) { |
| 622 | ModuleLiteral* module = declaration->module()->AsModuleLiteral(); |
| 623 | if (module != NULL) { |
| 624 | Comment cmnt(masm_, "[ Link nested modules"); |
| 625 | Scope* scope = module->body()->scope(); |
| 626 | Interface* interface = scope->interface(); |
| 627 | ASSERT(interface->IsModule() && interface->IsFrozen()); |
| 628 | |
| 629 | interface->Allocate(scope->module_var()->index()); |
| 630 | |
| 631 | // Set up module context. |
| 632 | ASSERT(scope->interface()->Index() >= 0); |
| 633 | __ Push(Smi::FromInt(scope->interface()->Index())); |
| 634 | __ Push(scope->GetScopeInfo()); |
| 635 | __ CallRuntime(Runtime::kPushModuleContext, 2); |
| 636 | StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 637 | context_register()); |
| 638 | |
| 639 | AllocateModules(scope->declarations()); |
| 640 | |
| 641 | // Pop module context. |
| 642 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 643 | // Update local stack frame context field. |
| 644 | StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 645 | context_register()); |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | |
| 652 | // Modules have their own local scope, represented by their own context. |
| 653 | // Module instance objects have an accessor for every export that forwards |
| 654 | // access to the respective slot from the module's context. (Exports that are |
| 655 | // modules themselves, however, are simple data properties.) |
| 656 | // |
| 657 | // All modules have a _hosting_ scope/context, which (currently) is the |
| 658 | // (innermost) enclosing global scope. To deal with recursion, nested modules |
| 659 | // are hosted by the same scope as global ones. |
| 660 | // |
| 661 | // For every (global or nested) module literal, the hosting context has an |
| 662 | // internal slot that points directly to the respective module context. This |
| 663 | // enables quick access to (statically resolved) module members by 2-dimensional |
| 664 | // access through the hosting context. For example, |
| 665 | // |
| 666 | // module A { |
| 667 | // let x; |
| 668 | // module B { let y; } |
| 669 | // } |
| 670 | // module C { let z; } |
| 671 | // |
| 672 | // allocates contexts as follows: |
| 673 | // |
| 674 | // [header| .A | .B | .C | A | C ] (global) |
| 675 | // | | | |
| 676 | // | | +-- [header| z ] (module) |
| 677 | // | | |
| 678 | // | +------- [header| y ] (module) |
| 679 | // | |
| 680 | // +------------ [header| x | B ] (module) |
| 681 | // |
| 682 | // Here, .A, .B, .C are the internal slots pointing to the hosted module |
| 683 | // contexts, whereas A, B, C hold the actual instance objects (note that every |
| 684 | // module context also points to the respective instance object through its |
| 685 | // extension slot in the header). |
| 686 | // |
| 687 | // To deal with arbitrary recursion and aliases between modules, |
| 688 | // they are created and initialized in several stages. Each stage applies to |
| 689 | // all modules in the hosting global scope, including nested ones. |
| 690 | // |
| 691 | // 1. Allocate: for each module _literal_, allocate the module contexts and |
| 692 | // respective instance object and wire them up. This happens in the |
| 693 | // PushModuleContext runtime function, as generated by AllocateModules |
| 694 | // (invoked by VisitDeclarations in the hosting scope). |
| 695 | // |
| 696 | // 2. Bind: for each module _declaration_ (i.e. literals as well as aliases), |
| 697 | // assign the respective instance object to respective local variables. This |
| 698 | // happens in VisitModuleDeclaration, and uses the instance objects created |
| 699 | // in the previous stage. |
| 700 | // For each module _literal_, this phase also constructs a module descriptor |
| 701 | // for the next stage. This happens in VisitModuleLiteral. |
| 702 | // |
| 703 | // 3. Populate: invoke the DeclareModules runtime function to populate each |
| 704 | // _instance_ object with accessors for it exports. This is generated by |
| 705 | // DeclareModules (invoked by VisitDeclarations in the hosting scope again), |
| 706 | // and uses the descriptors generated in the previous stage. |
| 707 | // |
| 708 | // 4. Initialize: execute the module bodies (and other code) in sequence. This |
| 709 | // happens by the separate statements generated for module bodies. To reenter |
| 710 | // the module scopes properly, the parser inserted ModuleStatements. |
| 711 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 712 | void FullCodeGenerator::VisitDeclarations( |
| 713 | ZoneList<Declaration*>* declarations) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 714 | Handle<FixedArray> saved_modules = modules_; |
| 715 | int saved_module_index = module_index_; |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 716 | ZoneList<Handle<Object> >* saved_globals = globals_; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 717 | ZoneList<Handle<Object> > inner_globals(10, zone()); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 718 | globals_ = &inner_globals; |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 719 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 720 | if (scope_->num_modules() != 0) { |
| 721 | // This is a scope hosting modules. Allocate a descriptor array to pass |
| 722 | // to the runtime for initialization. |
| 723 | Comment cmnt(masm_, "[ Allocate modules"); |
| 724 | ASSERT(scope_->is_global_scope()); |
| 725 | modules_ = |
| 726 | isolate()->factory()->NewFixedArray(scope_->num_modules(), TENURED); |
| 727 | module_index_ = 0; |
| 728 | |
| 729 | // Generate code for allocating all modules, including nested ones. |
| 730 | // The allocated contexts are stored in internal variables in this scope. |
| 731 | AllocateModules(declarations); |
| 732 | } |
| 733 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 734 | AstVisitor::VisitDeclarations(declarations); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 735 | |
| 736 | if (scope_->num_modules() != 0) { |
| 737 | // Initialize modules from descriptor array. |
| 738 | ASSERT(module_index_ == modules_->length()); |
| 739 | DeclareModules(modules_); |
| 740 | modules_ = saved_modules; |
| 741 | module_index_ = saved_module_index; |
| 742 | } |
| 743 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 744 | if (!globals_->is_empty()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 745 | // Invoke the platform-dependent code generator to do the actual |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 746 | // declaration of the global functions and variables. |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 747 | Handle<FixedArray> array = |
| 748 | isolate()->factory()->NewFixedArray(globals_->length(), TENURED); |
| 749 | for (int i = 0; i < globals_->length(); ++i) |
| 750 | array->set(i, *globals_->at(i)); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 751 | DeclareGlobals(array); |
| 752 | } |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 753 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 754 | globals_ = saved_globals; |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | |
| 758 | void FullCodeGenerator::VisitModuleLiteral(ModuleLiteral* module) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 759 | Block* block = module->body(); |
| 760 | Scope* saved_scope = scope(); |
| 761 | scope_ = block->scope(); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 762 | Interface* interface = scope_->interface(); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 763 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 764 | Comment cmnt(masm_, "[ ModuleLiteral"); |
| 765 | SetStatementPosition(block); |
| 766 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 767 | ASSERT(!modules_.is_null()); |
| 768 | ASSERT(module_index_ < modules_->length()); |
| 769 | int index = module_index_++; |
| 770 | |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 771 | // Set up module context. |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 772 | ASSERT(interface->Index() >= 0); |
| 773 | __ Push(Smi::FromInt(interface->Index())); |
| 774 | __ Push(Smi::FromInt(0)); |
| 775 | __ CallRuntime(Runtime::kPushModuleContext, 2); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 776 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 777 | |
| 778 | { |
| 779 | Comment cmnt(masm_, "[ Declarations"); |
| 780 | VisitDeclarations(scope_->declarations()); |
| 781 | } |
| 782 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 783 | // Populate the module description. |
| 784 | Handle<ModuleInfo> description = |
| 785 | ModuleInfo::Create(isolate(), interface, scope_); |
| 786 | modules_->set(index, *description); |
| 787 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 788 | scope_ = saved_scope; |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 789 | // Pop module context. |
| 790 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 791 | // Update local stack frame context field. |
| 792 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | |
| 796 | void FullCodeGenerator::VisitModuleVariable(ModuleVariable* module) { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 797 | // Nothing to do. |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 798 | // The instance object is resolved statically through the module's interface. |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | |
| 802 | void FullCodeGenerator::VisitModulePath(ModulePath* module) { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 803 | // Nothing to do. |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 804 | // The instance object is resolved statically through the module's interface. |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 808 | void FullCodeGenerator::VisitModuleUrl(ModuleUrl* module) { |
| 809 | // TODO(rossberg): dummy allocation for now. |
| 810 | Scope* scope = module->body()->scope(); |
| 811 | Interface* interface = scope_->interface(); |
| 812 | |
| 813 | ASSERT(interface->IsModule() && interface->IsFrozen()); |
| 814 | ASSERT(!modules_.is_null()); |
| 815 | ASSERT(module_index_ < modules_->length()); |
| 816 | interface->Allocate(scope->module_var()->index()); |
| 817 | int index = module_index_++; |
| 818 | |
| 819 | Handle<ModuleInfo> description = |
| 820 | ModuleInfo::Create(isolate(), interface, scope_); |
| 821 | modules_->set(index, *description); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 825 | int FullCodeGenerator::DeclareGlobalsFlags() { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 826 | ASSERT(DeclareGlobalsLanguageMode::is_valid(language_mode())); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 827 | return DeclareGlobalsEvalFlag::encode(is_eval()) | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 828 | DeclareGlobalsNativeFlag::encode(is_native()) | |
| 829 | DeclareGlobalsLanguageMode::encode(language_mode()); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 833 | void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 834 | CodeGenerator::RecordPositions(masm_, fun->start_position()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | |
| 838 | void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 839 | CodeGenerator::RecordPositions(masm_, fun->end_position() - 1); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | |
| 843 | void FullCodeGenerator::SetStatementPosition(Statement* stmt) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 844 | #ifdef ENABLE_DEBUGGER_SUPPORT |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 845 | if (!isolate()->debugger()->IsDebuggerActive()) { |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 846 | CodeGenerator::RecordPositions(masm_, stmt->position()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 847 | } else { |
| 848 | // Check if the statement will be breakable without adding a debug break |
| 849 | // slot. |
machenbach@chromium.org | 6d26cbb | 2014-01-22 10:50:56 +0000 | [diff] [blame] | 850 | BreakableStatementChecker checker(zone()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 851 | checker.Check(stmt); |
| 852 | // Record the statement position right here if the statement is not |
| 853 | // breakable. For breakable statements the actual recording of the |
| 854 | // position will be postponed to the breakable code (typically an IC). |
| 855 | bool position_recorded = CodeGenerator::RecordPositions( |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 856 | masm_, stmt->position(), !checker.is_breakable()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 857 | // If the position recording did record a new position generate a debug |
| 858 | // break slot to make the statement breakable. |
| 859 | if (position_recorded) { |
| 860 | Debug::GenerateSlot(masm_); |
| 861 | } |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 862 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 863 | #else |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 864 | CodeGenerator::RecordPositions(masm_, stmt->position()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 865 | #endif |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 869 | void FullCodeGenerator::SetExpressionPosition(Expression* expr) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 870 | #ifdef ENABLE_DEBUGGER_SUPPORT |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 871 | if (!isolate()->debugger()->IsDebuggerActive()) { |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 872 | CodeGenerator::RecordPositions(masm_, expr->position()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 873 | } else { |
| 874 | // Check if the expression will be breakable without adding a debug break |
| 875 | // slot. |
machenbach@chromium.org | 6d26cbb | 2014-01-22 10:50:56 +0000 | [diff] [blame] | 876 | BreakableStatementChecker checker(zone()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 877 | checker.Check(expr); |
| 878 | // Record a statement position right here if the expression is not |
| 879 | // breakable. For breakable expressions the actual recording of the |
| 880 | // position will be postponed to the breakable code (typically an IC). |
| 881 | // NOTE this will record a statement position for something which might |
| 882 | // not be a statement. As stepping in the debugger will only stop at |
| 883 | // statement positions this is used for e.g. the condition expression of |
| 884 | // a do while loop. |
| 885 | bool position_recorded = CodeGenerator::RecordPositions( |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 886 | masm_, expr->position(), !checker.is_breakable()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 887 | // If the position recording did record a new position generate a debug |
| 888 | // break slot to make the statement breakable. |
| 889 | if (position_recorded) { |
| 890 | Debug::GenerateSlot(masm_); |
| 891 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 892 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 893 | #else |
| 894 | CodeGenerator::RecordPositions(masm_, pos); |
| 895 | #endif |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | |
| 899 | void FullCodeGenerator::SetStatementPosition(int pos) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 900 | CodeGenerator::RecordPositions(masm_, pos); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 904 | void FullCodeGenerator::SetSourcePosition(int pos) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 905 | if (pos != RelocInfo::kNoPosition) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 906 | masm_->positions_recorder()->RecordPosition(pos); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
| 910 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 911 | // Lookup table for code generators for special runtime calls which are |
| 912 | // generated inline. |
| 913 | #define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \ |
| 914 | &FullCodeGenerator::Emit##Name, |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 915 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 916 | const FullCodeGenerator::InlineFunctionGenerator |
| 917 | FullCodeGenerator::kInlineFunctionGenerators[] = { |
| 918 | INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 919 | INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 920 | }; |
| 921 | #undef INLINE_FUNCTION_GENERATOR_ADDRESS |
| 922 | |
| 923 | |
| 924 | FullCodeGenerator::InlineFunctionGenerator |
| 925 | FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 926 | int lookup_index = |
| 927 | static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction); |
| 928 | ASSERT(lookup_index >= 0); |
| 929 | ASSERT(static_cast<size_t>(lookup_index) < |
| 930 | ARRAY_SIZE(kInlineFunctionGenerators)); |
| 931 | return kInlineFunctionGenerators[lookup_index]; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 935 | void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* expr) { |
| 936 | const Runtime::Function* function = expr->function(); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 937 | ASSERT(function != NULL); |
| 938 | ASSERT(function->intrinsic_type == Runtime::INLINE); |
| 939 | InlineFunctionGenerator generator = |
| 940 | FindInlineFunctionGenerator(function->function_id); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 941 | ((*this).*(generator))(expr); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | |
verwaest@chromium.org | 8a00e82 | 2013-06-10 15:11:22 +0000 | [diff] [blame] | 945 | void FullCodeGenerator::EmitGeneratorNext(CallRuntime* expr) { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 946 | ZoneList<Expression*>* args = expr->arguments(); |
| 947 | ASSERT(args->length() == 2); |
verwaest@chromium.org | 8a00e82 | 2013-06-10 15:11:22 +0000 | [diff] [blame] | 948 | EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::NEXT); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | |
| 952 | void FullCodeGenerator::EmitGeneratorThrow(CallRuntime* expr) { |
| 953 | ZoneList<Expression*>* args = expr->arguments(); |
| 954 | ASSERT(args->length() == 2); |
| 955 | EmitGeneratorResume(args->at(0), args->at(1), JSGeneratorObject::THROW); |
| 956 | } |
| 957 | |
| 958 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 959 | void FullCodeGenerator::EmitDebugBreakInOptimizedCode(CallRuntime* expr) { |
| 960 | context()->Plug(handle(Smi::FromInt(0), isolate())); |
| 961 | } |
| 962 | |
| 963 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 964 | void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 965 | switch (expr->op()) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 966 | case Token::COMMA: |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 967 | return VisitComma(expr); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 968 | case Token::OR: |
| 969 | case Token::AND: |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 970 | return VisitLogicalExpression(expr); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 971 | default: |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 972 | return VisitArithmeticExpression(expr); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 973 | } |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 977 | void FullCodeGenerator::VisitInDuplicateContext(Expression* expr) { |
| 978 | if (context()->IsEffect()) { |
| 979 | VisitForEffect(expr); |
| 980 | } else if (context()->IsAccumulatorValue()) { |
| 981 | VisitForAccumulatorValue(expr); |
| 982 | } else if (context()->IsStackValue()) { |
| 983 | VisitForStackValue(expr); |
| 984 | } else if (context()->IsTest()) { |
| 985 | const TestContext* test = TestContext::cast(context()); |
| 986 | VisitForControl(expr, test->true_label(), test->false_label(), |
| 987 | test->fall_through()); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 992 | void FullCodeGenerator::VisitComma(BinaryOperation* expr) { |
| 993 | Comment cmnt(masm_, "[ Comma"); |
| 994 | VisitForEffect(expr->left()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 995 | VisitInDuplicateContext(expr->right()); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 996 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 997 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 998 | |
| 999 | void FullCodeGenerator::VisitLogicalExpression(BinaryOperation* expr) { |
| 1000 | bool is_logical_and = expr->op() == Token::AND; |
| 1001 | Comment cmnt(masm_, is_logical_and ? "[ Logical AND" : "[ Logical OR"); |
| 1002 | Expression* left = expr->left(); |
| 1003 | Expression* right = expr->right(); |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 1004 | BailoutId right_id = expr->RightId(); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1005 | Label done; |
| 1006 | |
| 1007 | if (context()->IsTest()) { |
| 1008 | Label eval_right; |
| 1009 | const TestContext* test = TestContext::cast(context()); |
| 1010 | if (is_logical_and) { |
| 1011 | VisitForControl(left, &eval_right, test->false_label(), &eval_right); |
| 1012 | } else { |
| 1013 | VisitForControl(left, test->true_label(), &eval_right, &eval_right); |
| 1014 | } |
| 1015 | PrepareForBailoutForId(right_id, NO_REGISTERS); |
| 1016 | __ bind(&eval_right); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1017 | |
| 1018 | } else if (context()->IsAccumulatorValue()) { |
| 1019 | VisitForAccumulatorValue(left); |
| 1020 | // We want the value in the accumulator for the test, and on the stack in |
| 1021 | // case we need it. |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 1022 | __ Push(result_register()); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1023 | Label discard, restore; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1024 | if (is_logical_and) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1025 | DoTest(left, &discard, &restore, &restore); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1026 | } else { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1027 | DoTest(left, &restore, &discard, &restore); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1028 | } |
| 1029 | __ bind(&restore); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 1030 | __ Pop(result_register()); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1031 | __ jmp(&done); |
| 1032 | __ bind(&discard); |
| 1033 | __ Drop(1); |
| 1034 | PrepareForBailoutForId(right_id, NO_REGISTERS); |
| 1035 | |
| 1036 | } else if (context()->IsStackValue()) { |
| 1037 | VisitForAccumulatorValue(left); |
| 1038 | // We want the value in the accumulator for the test, and on the stack in |
| 1039 | // case we need it. |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 1040 | __ Push(result_register()); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1041 | Label discard; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1042 | if (is_logical_and) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1043 | DoTest(left, &discard, &done, &discard); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1044 | } else { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1045 | DoTest(left, &done, &discard, &discard); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1046 | } |
| 1047 | __ bind(&discard); |
| 1048 | __ Drop(1); |
| 1049 | PrepareForBailoutForId(right_id, NO_REGISTERS); |
| 1050 | |
| 1051 | } else { |
| 1052 | ASSERT(context()->IsEffect()); |
| 1053 | Label eval_right; |
| 1054 | if (is_logical_and) { |
| 1055 | VisitForControl(left, &eval_right, &done, &eval_right); |
| 1056 | } else { |
| 1057 | VisitForControl(left, &done, &eval_right, &eval_right); |
| 1058 | } |
| 1059 | PrepareForBailoutForId(right_id, NO_REGISTERS); |
| 1060 | __ bind(&eval_right); |
| 1061 | } |
| 1062 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1063 | VisitInDuplicateContext(right); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1064 | __ bind(&done); |
| 1065 | } |
| 1066 | |
| 1067 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1068 | void FullCodeGenerator::VisitArithmeticExpression(BinaryOperation* expr) { |
| 1069 | Token::Value op = expr->op(); |
| 1070 | Comment cmnt(masm_, "[ ArithmeticExpression"); |
| 1071 | Expression* left = expr->left(); |
| 1072 | Expression* right = expr->right(); |
| 1073 | OverwriteMode mode = |
| 1074 | left->ResultOverwriteAllowed() |
| 1075 | ? OVERWRITE_LEFT |
| 1076 | : (right->ResultOverwriteAllowed() ? OVERWRITE_RIGHT : NO_OVERWRITE); |
| 1077 | |
| 1078 | VisitForStackValue(left); |
| 1079 | VisitForAccumulatorValue(right); |
| 1080 | |
| 1081 | SetSourcePosition(expr->position()); |
| 1082 | if (ShouldInlineSmiCase(op)) { |
| 1083 | EmitInlineSmiBinaryOp(expr, op, mode, left, right); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1084 | } else { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1085 | EmitBinaryOp(expr, op, mode); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1090 | void FullCodeGenerator::VisitBlock(Block* stmt) { |
| 1091 | Comment cmnt(masm_, "[ Block"); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1092 | NestedBlock nested_block(this, stmt); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1093 | SetStatementPosition(stmt); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1094 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1095 | Scope* saved_scope = scope(); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1096 | // Push a block context when entering a block with block scoped variables. |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1097 | if (stmt->scope() != NULL) { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1098 | scope_ = stmt->scope(); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1099 | ASSERT(!scope_->is_module_scope()); |
| 1100 | { Comment cmnt(masm_, "[ Extend block context"); |
machenbach@chromium.org | 05150ab | 2014-01-29 08:13:29 +0000 | [diff] [blame^] | 1101 | __ Push(scope_->GetScopeInfo()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1102 | PushFunctionArgumentForContextAllocation(); |
machenbach@chromium.org | 05150ab | 2014-01-29 08:13:29 +0000 | [diff] [blame^] | 1103 | __ CallRuntime(Runtime::kPushBlockContext, 2); |
svenpanne@chromium.org | a8bb4d9 | 2011-10-10 13:20:40 +0000 | [diff] [blame] | 1104 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1105 | // Replace the context stored in the frame. |
| 1106 | StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 1107 | context_register()); |
| 1108 | } |
| 1109 | { Comment cmnt(masm_, "[ Declarations"); |
| 1110 | VisitDeclarations(scope_->declarations()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1111 | } |
| 1112 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1113 | |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 1114 | PrepareForBailoutForId(stmt->EntryId(), NO_REGISTERS); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1115 | VisitStatements(stmt->statements()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1116 | scope_ = saved_scope; |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1117 | __ bind(nested_block.break_label()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1118 | PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1119 | |
| 1120 | // Pop block context if necessary. |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1121 | if (stmt->scope() != NULL) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1122 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 1123 | // Update local stack frame context field. |
| 1124 | StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 1125 | context_register()); |
| 1126 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1130 | void FullCodeGenerator::VisitModuleStatement(ModuleStatement* stmt) { |
| 1131 | Comment cmnt(masm_, "[ Module context"); |
| 1132 | |
| 1133 | __ Push(Smi::FromInt(stmt->proxy()->interface()->Index())); |
| 1134 | __ Push(Smi::FromInt(0)); |
| 1135 | __ CallRuntime(Runtime::kPushModuleContext, 2); |
| 1136 | StoreToFrameField( |
| 1137 | StandardFrameConstants::kContextOffset, context_register()); |
| 1138 | |
| 1139 | Scope* saved_scope = scope_; |
| 1140 | scope_ = stmt->body()->scope(); |
| 1141 | VisitStatements(stmt->body()->statements()); |
| 1142 | scope_ = saved_scope; |
| 1143 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 1144 | // Update local stack frame context field. |
| 1145 | StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 1146 | context_register()); |
| 1147 | } |
| 1148 | |
| 1149 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1150 | void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) { |
| 1151 | Comment cmnt(masm_, "[ ExpressionStatement"); |
| 1152 | SetStatementPosition(stmt); |
| 1153 | VisitForEffect(stmt->expression()); |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) { |
| 1158 | Comment cmnt(masm_, "[ EmptyStatement"); |
| 1159 | SetStatementPosition(stmt); |
| 1160 | } |
| 1161 | |
| 1162 | |
| 1163 | void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) { |
| 1164 | Comment cmnt(masm_, "[ IfStatement"); |
| 1165 | SetStatementPosition(stmt); |
| 1166 | Label then_part, else_part, done; |
| 1167 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1168 | if (stmt->HasElseStatement()) { |
| 1169 | VisitForControl(stmt->condition(), &then_part, &else_part, &then_part); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1170 | PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1171 | __ bind(&then_part); |
| 1172 | Visit(stmt->then_statement()); |
| 1173 | __ jmp(&done); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1174 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1175 | PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1176 | __ bind(&else_part); |
| 1177 | Visit(stmt->else_statement()); |
| 1178 | } else { |
| 1179 | VisitForControl(stmt->condition(), &then_part, &done, &then_part); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1180 | PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1181 | __ bind(&then_part); |
| 1182 | Visit(stmt->then_statement()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1183 | |
| 1184 | PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1185 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1186 | __ bind(&done); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1187 | PrepareForBailoutForId(stmt->IfId(), NO_REGISTERS); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | |
| 1191 | void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) { |
| 1192 | Comment cmnt(masm_, "[ ContinueStatement"); |
| 1193 | SetStatementPosition(stmt); |
| 1194 | NestedStatement* current = nesting_stack_; |
| 1195 | int stack_depth = 0; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1196 | int context_length = 0; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1197 | // When continuing, we clobber the unpredictable value in the accumulator |
| 1198 | // with one that's safe for GC. If we hit an exit from the try block of |
| 1199 | // try...finally on our way out, we will unconditionally preserve the |
| 1200 | // accumulator on the stack. |
| 1201 | ClearAccumulator(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1202 | while (!current->IsContinueTarget(stmt->target())) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1203 | current = current->Exit(&stack_depth, &context_length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1204 | } |
| 1205 | __ Drop(stack_depth); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1206 | if (context_length > 0) { |
| 1207 | while (context_length > 0) { |
| 1208 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 1209 | --context_length; |
| 1210 | } |
| 1211 | StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 1212 | context_register()); |
| 1213 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1214 | |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1215 | __ jmp(current->AsIteration()->continue_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | |
| 1219 | void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) { |
| 1220 | Comment cmnt(masm_, "[ BreakStatement"); |
| 1221 | SetStatementPosition(stmt); |
| 1222 | NestedStatement* current = nesting_stack_; |
| 1223 | int stack_depth = 0; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1224 | int context_length = 0; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1225 | // When breaking, we clobber the unpredictable value in the accumulator |
| 1226 | // with one that's safe for GC. If we hit an exit from the try block of |
| 1227 | // try...finally on our way out, we will unconditionally preserve the |
| 1228 | // accumulator on the stack. |
| 1229 | ClearAccumulator(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1230 | while (!current->IsBreakTarget(stmt->target())) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1231 | current = current->Exit(&stack_depth, &context_length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1232 | } |
| 1233 | __ Drop(stack_depth); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1234 | if (context_length > 0) { |
| 1235 | while (context_length > 0) { |
| 1236 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 1237 | --context_length; |
| 1238 | } |
| 1239 | StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 1240 | context_register()); |
| 1241 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1242 | |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1243 | __ jmp(current->AsBreakable()->break_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 1247 | void FullCodeGenerator::EmitUnwindBeforeReturn() { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1248 | NestedStatement* current = nesting_stack_; |
| 1249 | int stack_depth = 0; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1250 | int context_length = 0; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1251 | while (current != NULL) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1252 | current = current->Exit(&stack_depth, &context_length); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1253 | } |
| 1254 | __ Drop(stack_depth); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 1255 | } |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1256 | |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 1257 | |
| 1258 | void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) { |
| 1259 | Comment cmnt(masm_, "[ ReturnStatement"); |
| 1260 | SetStatementPosition(stmt); |
| 1261 | Expression* expr = stmt->expression(); |
| 1262 | VisitForAccumulatorValue(expr); |
| 1263 | EmitUnwindBeforeReturn(); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1264 | EmitReturnSequence(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1268 | void FullCodeGenerator::VisitWithStatement(WithStatement* stmt) { |
| 1269 | Comment cmnt(masm_, "[ WithStatement"); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1270 | SetStatementPosition(stmt); |
| 1271 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1272 | VisitForStackValue(stmt->expression()); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 1273 | PushFunctionArgumentForContextAllocation(); |
| 1274 | __ CallRuntime(Runtime::kPushWithContext, 2); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1275 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1276 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 1277 | Scope* saved_scope = scope(); |
| 1278 | scope_ = stmt->scope(); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1279 | { WithOrCatch body(this); |
| 1280 | Visit(stmt->statement()); |
| 1281 | } |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 1282 | scope_ = saved_scope; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1283 | |
| 1284 | // Pop context. |
| 1285 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 1286 | // Update local stack frame context field. |
| 1287 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1291 | void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 1292 | Comment cmnt(masm_, "[ DoWhileStatement"); |
| 1293 | SetStatementPosition(stmt); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1294 | Label body, book_keeping; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1295 | |
| 1296 | Iteration loop_statement(this, stmt); |
| 1297 | increment_loop_depth(); |
| 1298 | |
| 1299 | __ bind(&body); |
| 1300 | Visit(stmt->body()); |
| 1301 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1302 | // Record the position of the do while condition and make sure it is |
| 1303 | // possible to break on the condition. |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1304 | __ bind(loop_statement.continue_label()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1305 | PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS); |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1306 | SetExpressionPosition(stmt->cond()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1307 | VisitForControl(stmt->cond(), |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1308 | &book_keeping, |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1309 | loop_statement.break_label(), |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1310 | &book_keeping); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1311 | |
| 1312 | // Check stack before looping. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1313 | PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1314 | __ bind(&book_keeping); |
rossberg@chromium.org | cddc71f | 2012-12-07 12:40:13 +0000 | [diff] [blame] | 1315 | EmitBackEdgeBookkeeping(stmt, &body); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1316 | __ jmp(&body); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1317 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1318 | PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1319 | __ bind(loop_statement.break_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1320 | decrement_loop_depth(); |
| 1321 | } |
| 1322 | |
| 1323 | |
| 1324 | void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) { |
| 1325 | Comment cmnt(masm_, "[ WhileStatement"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1326 | Label test, body; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1327 | |
| 1328 | Iteration loop_statement(this, stmt); |
| 1329 | increment_loop_depth(); |
| 1330 | |
| 1331 | // Emit the test at the bottom of the loop. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1332 | __ jmp(&test); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1333 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1334 | PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1335 | __ bind(&body); |
| 1336 | Visit(stmt->body()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1337 | |
| 1338 | // Emit the statement position here as this is where the while |
| 1339 | // statement code starts. |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1340 | __ bind(loop_statement.continue_label()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1341 | SetStatementPosition(stmt); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1342 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1343 | // Check stack before looping. |
rossberg@chromium.org | cddc71f | 2012-12-07 12:40:13 +0000 | [diff] [blame] | 1344 | EmitBackEdgeBookkeeping(stmt, &body); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1345 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1346 | __ bind(&test); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1347 | VisitForControl(stmt->cond(), |
| 1348 | &body, |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1349 | loop_statement.break_label(), |
| 1350 | loop_statement.break_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1351 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1352 | PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1353 | __ bind(loop_statement.break_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1354 | decrement_loop_depth(); |
| 1355 | } |
| 1356 | |
| 1357 | |
| 1358 | void FullCodeGenerator::VisitForStatement(ForStatement* stmt) { |
| 1359 | Comment cmnt(masm_, "[ ForStatement"); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1360 | Label test, body; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1361 | |
| 1362 | Iteration loop_statement(this, stmt); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1363 | |
| 1364 | // Set statement position for a break slot before entering the for-body. |
| 1365 | SetStatementPosition(stmt); |
| 1366 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1367 | if (stmt->init() != NULL) { |
| 1368 | Visit(stmt->init()); |
| 1369 | } |
| 1370 | |
| 1371 | increment_loop_depth(); |
| 1372 | // Emit the test at the bottom of the loop (even if empty). |
| 1373 | __ jmp(&test); |
| 1374 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1375 | PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1376 | __ bind(&body); |
| 1377 | Visit(stmt->body()); |
| 1378 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1379 | PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1380 | __ bind(loop_statement.continue_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1381 | if (stmt->next() != NULL) { |
| 1382 | Visit(stmt->next()); |
| 1383 | } |
| 1384 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1385 | // Emit the statement position here as this is where the for |
| 1386 | // statement code starts. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 1387 | SetStatementPosition(stmt); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1388 | |
| 1389 | // Check stack before looping. |
rossberg@chromium.org | cddc71f | 2012-12-07 12:40:13 +0000 | [diff] [blame] | 1390 | EmitBackEdgeBookkeeping(stmt, &body); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1391 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1392 | __ bind(&test); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1393 | if (stmt->cond() != NULL) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1394 | VisitForControl(stmt->cond(), |
| 1395 | &body, |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1396 | loop_statement.break_label(), |
| 1397 | loop_statement.break_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1398 | } else { |
| 1399 | __ jmp(&body); |
| 1400 | } |
| 1401 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1402 | PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 1403 | __ bind(loop_statement.break_label()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1404 | decrement_loop_depth(); |
| 1405 | } |
| 1406 | |
| 1407 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1408 | void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 1409 | Comment cmnt(masm_, "[ TryCatchStatement"); |
| 1410 | SetStatementPosition(stmt); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1411 | // The try block adds a handler to the exception handler chain before |
| 1412 | // entering, and removes it again when exiting normally. If an exception |
| 1413 | // is thrown during execution of the try block, the handler is consumed |
| 1414 | // and control is passed to the catch block with the exception in the |
| 1415 | // result register. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1416 | |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1417 | Label try_entry, handler_entry, exit; |
| 1418 | __ jmp(&try_entry); |
| 1419 | __ bind(&handler_entry); |
| 1420 | handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos())); |
| 1421 | // Exception handler code, the exception is in the result register. |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1422 | // Extend the context before executing the catch block. |
| 1423 | { Comment cmnt(masm_, "[ Extend catch context"); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1424 | __ Push(stmt->variable()->name()); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 1425 | __ Push(result_register()); |
vegorov@chromium.org | 3cf4731 | 2011-06-29 13:20:01 +0000 | [diff] [blame] | 1426 | PushFunctionArgumentForContextAllocation(); |
| 1427 | __ CallRuntime(Runtime::kPushCatchContext, 3); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1428 | StoreToFrameField(StandardFrameConstants::kContextOffset, |
| 1429 | context_register()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1432 | Scope* saved_scope = scope(); |
| 1433 | scope_ = stmt->scope(); |
| 1434 | ASSERT(scope_->declarations()->is_empty()); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1435 | { WithOrCatch catch_body(this); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1436 | Visit(stmt->catch_block()); |
| 1437 | } |
ricow@chromium.org | 55ee807 | 2011-09-08 16:33:10 +0000 | [diff] [blame] | 1438 | // Restore the context. |
| 1439 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 1440 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1441 | scope_ = saved_scope; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1442 | __ jmp(&exit); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1443 | |
| 1444 | // Try block code. Sets up the exception handler chain. |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1445 | __ bind(&try_entry); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1446 | __ PushTryHandler(StackHandler::CATCH, stmt->index()); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1447 | { TryCatch try_body(this); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1448 | Visit(stmt->try_block()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1449 | } |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1450 | __ PopTryHandler(); |
| 1451 | __ bind(&exit); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
| 1456 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
| 1457 | SetStatementPosition(stmt); |
| 1458 | // Try finally is compiled by setting up a try-handler on the stack while |
| 1459 | // executing the try body, and removing it again afterwards. |
| 1460 | // |
| 1461 | // The try-finally construct can enter the finally block in three ways: |
| 1462 | // 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] | 1463 | // calls the finally block code before continuing. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1464 | // 2. By exiting the try-block with a function-local control flow transfer |
| 1465 | // (break/continue/return). The site of the, e.g., break removes the |
| 1466 | // try handler and calls the finally block code before continuing |
| 1467 | // its outward control transfer. |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1468 | // 3. By exiting the try-block with a thrown exception. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1469 | // This can happen in nested function calls. It traverses the try-handler |
| 1470 | // chain and consumes the try-handler entry before jumping to the |
| 1471 | // handler code. The handler code then calls the finally-block before |
| 1472 | // rethrowing the exception. |
| 1473 | // |
| 1474 | // The finally block must assume a return address on top of the stack |
| 1475 | // (or in the link register on ARM chips) and a value (return value or |
| 1476 | // exception) in the result register (rax/eax/r0), both of which must |
| 1477 | // be preserved. The return address isn't GC-safe, so it should be |
| 1478 | // cooked before GC. |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1479 | Label try_entry, handler_entry, finally_entry; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1480 | |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1481 | // Jump to try-handler setup and try-block code. |
| 1482 | __ jmp(&try_entry); |
| 1483 | __ bind(&handler_entry); |
| 1484 | handler_table()->set(stmt->index(), Smi::FromInt(handler_entry.pos())); |
| 1485 | // Exception handler code. This code is only executed when an exception |
| 1486 | // is thrown. The exception is in the result register, and must be |
| 1487 | // preserved by the finally block. Call the finally block and then |
| 1488 | // rethrow the exception if it returns. |
| 1489 | __ Call(&finally_entry); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 1490 | __ Push(result_register()); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1491 | __ CallRuntime(Runtime::kReThrow, 1); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1492 | |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1493 | // Finally block implementation. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1494 | __ bind(&finally_entry); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1495 | EnterFinallyBlock(); |
| 1496 | { Finally finally_body(this); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1497 | Visit(stmt->finally_block()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1498 | } |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1499 | ExitFinallyBlock(); // Return to the calling code. |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1500 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1501 | // Set up try handler. |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1502 | __ bind(&try_entry); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1503 | __ PushTryHandler(StackHandler::FINALLY, stmt->index()); |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1504 | { TryFinally try_body(this, &finally_entry); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1505 | Visit(stmt->try_block()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1506 | } |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1507 | __ PopTryHandler(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1508 | // Execute the finally block on the way out. Clobber the unpredictable |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 1509 | // value in the result register with one that's safe for GC because the |
| 1510 | // finally block will unconditionally preserve the result register on the |
| 1511 | // stack. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1512 | ClearAccumulator(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1513 | __ Call(&finally_entry); |
| 1514 | } |
| 1515 | |
| 1516 | |
| 1517 | void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) { |
| 1518 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1519 | Comment cmnt(masm_, "[ DebuggerStatement"); |
| 1520 | SetStatementPosition(stmt); |
| 1521 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 1522 | __ DebugBreak(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1523 | // Ignore the return value. |
| 1524 | #endif |
| 1525 | } |
| 1526 | |
| 1527 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1528 | void FullCodeGenerator::VisitCaseClause(CaseClause* clause) { |
| 1529 | UNREACHABLE(); |
| 1530 | } |
| 1531 | |
| 1532 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1533 | void FullCodeGenerator::VisitConditional(Conditional* expr) { |
| 1534 | Comment cmnt(masm_, "[ Conditional"); |
| 1535 | Label true_case, false_case, done; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1536 | VisitForControl(expr->condition(), &true_case, &false_case, &true_case); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1537 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1538 | PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1539 | __ bind(&true_case); |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1540 | SetExpressionPosition(expr->then_expression()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1541 | if (context()->IsTest()) { |
| 1542 | const TestContext* for_test = TestContext::cast(context()); |
| 1543 | VisitForControl(expr->then_expression(), |
| 1544 | for_test->true_label(), |
| 1545 | for_test->false_label(), |
| 1546 | NULL); |
| 1547 | } else { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1548 | VisitInDuplicateContext(expr->then_expression()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1549 | __ jmp(&done); |
| 1550 | } |
| 1551 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1552 | PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1553 | __ bind(&false_case); |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 1554 | SetExpressionPosition(expr->else_expression()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1555 | VisitInDuplicateContext(expr->else_expression()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1556 | // 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] | 1557 | if (!context()->IsTest()) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1558 | __ bind(&done); |
| 1559 | } |
| 1560 | } |
| 1561 | |
| 1562 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1563 | void FullCodeGenerator::VisitLiteral(Literal* expr) { |
| 1564 | Comment cmnt(masm_, "[ Literal"); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1565 | context()->Plug(expr->value()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1569 | void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 1570 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 1571 | |
| 1572 | // Build the function boilerplate and instantiate it. |
| 1573 | Handle<SharedFunctionInfo> function_info = |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1574 | Compiler::BuildFunctionInfo(expr, script()); |
| 1575 | if (function_info.is_null()) { |
| 1576 | SetStackOverflow(); |
| 1577 | return; |
| 1578 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1579 | EmitNewClosure(function_info, expr->pretenure()); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 1583 | void FullCodeGenerator::VisitNativeFunctionLiteral( |
| 1584 | NativeFunctionLiteral* expr) { |
| 1585 | Comment cmnt(masm_, "[ NativeFunctionLiteral"); |
| 1586 | |
| 1587 | // Compute the function template for the native function. |
| 1588 | Handle<String> name = expr->name(); |
| 1589 | v8::Handle<v8::FunctionTemplate> fun_template = |
machenbach@chromium.org | 37be408 | 2013-11-26 13:50:38 +0000 | [diff] [blame] | 1590 | expr->extension()->GetNativeFunctionTemplate( |
| 1591 | reinterpret_cast<v8::Isolate*>(isolate()), v8::Utils::ToLocal(name)); |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 1592 | ASSERT(!fun_template.IsEmpty()); |
| 1593 | |
| 1594 | // Instantiate the function and create a shared function info from it. |
| 1595 | Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction()); |
| 1596 | const int literals = fun->NumberOfLiterals(); |
| 1597 | Handle<Code> code = Handle<Code>(fun->shared()->code()); |
| 1598 | Handle<Code> construct_stub = Handle<Code>(fun->shared()->construct_stub()); |
| 1599 | bool is_generator = false; |
| 1600 | Handle<SharedFunctionInfo> shared = |
| 1601 | isolate()->factory()->NewSharedFunctionInfo(name, literals, is_generator, |
| 1602 | code, Handle<ScopeInfo>(fun->shared()->scope_info())); |
| 1603 | shared->set_construct_stub(*construct_stub); |
| 1604 | |
| 1605 | // Copy the function data to the shared function info. |
| 1606 | shared->set_function_data(fun->shared()->function_data()); |
| 1607 | int parameters = fun->shared()->formal_parameter_count(); |
| 1608 | shared->set_formal_parameter_count(parameters); |
| 1609 | |
| 1610 | EmitNewClosure(shared, false); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1614 | void FullCodeGenerator::VisitThrow(Throw* expr) { |
| 1615 | Comment cmnt(masm_, "[ Throw"); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1616 | VisitForStackValue(expr->exception()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1617 | __ CallRuntime(Runtime::kThrow, 1); |
| 1618 | // Never returns here. |
| 1619 | } |
| 1620 | |
| 1621 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1622 | FullCodeGenerator::NestedStatement* FullCodeGenerator::TryCatch::Exit( |
| 1623 | int* stack_depth, |
| 1624 | int* context_length) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1625 | // The macros used here must preserve the result register. |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1626 | __ Drop(*stack_depth); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1627 | __ PopTryHandler(); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1628 | *stack_depth = 0; |
| 1629 | return previous_; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1632 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1633 | bool FullCodeGenerator::TryLiteralCompare(CompareOperation* expr) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1634 | Expression* sub_expr; |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1635 | Handle<String> check; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1636 | if (expr->IsLiteralCompareTypeof(&sub_expr, &check)) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1637 | EmitLiteralCompareTypeof(expr, sub_expr, check); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1638 | return true; |
| 1639 | } |
| 1640 | |
jkummerow@chromium.org | 96a3c51 | 2013-07-18 17:02:47 +0000 | [diff] [blame] | 1641 | if (expr->IsLiteralCompareUndefined(&sub_expr, isolate())) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1642 | EmitLiteralCompareNil(expr, sub_expr, kUndefinedValue); |
| 1643 | return true; |
| 1644 | } |
| 1645 | |
| 1646 | if (expr->IsLiteralCompareNull(&sub_expr)) { |
| 1647 | EmitLiteralCompareNil(expr, sub_expr, kNullValue); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 1648 | return true; |
| 1649 | } |
| 1650 | |
| 1651 | return false; |
| 1652 | } |
| 1653 | |
| 1654 | |
yangguo@chromium.org | 4954674 | 2013-12-23 16:17:49 +0000 | [diff] [blame] | 1655 | void BackEdgeTable::Patch(Isolate* isolate, Code* unoptimized) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1656 | DisallowHeapAllocation no_gc; |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1657 | Code* patch = isolate->builtins()->builtin(Builtins::kOnStackReplacement); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1658 | |
| 1659 | // Iterate over the back edge table and patch every interrupt |
| 1660 | // call to an unconditional call to the replacement code. |
| 1661 | int loop_nesting_level = unoptimized->allow_osr_at_loop_nesting_level(); |
| 1662 | |
| 1663 | BackEdgeTable back_edges(unoptimized, &no_gc); |
| 1664 | for (uint32_t i = 0; i < back_edges.length(); i++) { |
| 1665 | if (static_cast<int>(back_edges.loop_depth(i)) == loop_nesting_level) { |
| 1666 | ASSERT_EQ(INTERRUPT, GetBackEdgeState(isolate, |
| 1667 | unoptimized, |
| 1668 | back_edges.pc(i))); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1669 | PatchAt(unoptimized, back_edges.pc(i), ON_STACK_REPLACEMENT, patch); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | unoptimized->set_back_edges_patched_for_osr(true); |
| 1674 | ASSERT(Verify(isolate, unoptimized, loop_nesting_level)); |
| 1675 | } |
| 1676 | |
| 1677 | |
yangguo@chromium.org | 4954674 | 2013-12-23 16:17:49 +0000 | [diff] [blame] | 1678 | void BackEdgeTable::Revert(Isolate* isolate, Code* unoptimized) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1679 | DisallowHeapAllocation no_gc; |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1680 | Code* patch = isolate->builtins()->builtin(Builtins::kInterruptCheck); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1681 | |
| 1682 | // Iterate over the back edge table and revert the patched interrupt calls. |
| 1683 | ASSERT(unoptimized->back_edges_patched_for_osr()); |
| 1684 | int loop_nesting_level = unoptimized->allow_osr_at_loop_nesting_level(); |
| 1685 | |
| 1686 | BackEdgeTable back_edges(unoptimized, &no_gc); |
| 1687 | for (uint32_t i = 0; i < back_edges.length(); i++) { |
| 1688 | if (static_cast<int>(back_edges.loop_depth(i)) <= loop_nesting_level) { |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1689 | ASSERT_NE(INTERRUPT, GetBackEdgeState(isolate, |
| 1690 | unoptimized, |
| 1691 | back_edges.pc(i))); |
| 1692 | PatchAt(unoptimized, back_edges.pc(i), INTERRUPT, patch); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | unoptimized->set_back_edges_patched_for_osr(false); |
| 1697 | unoptimized->set_allow_osr_at_loop_nesting_level(0); |
| 1698 | // Assert that none of the back edges are patched anymore. |
| 1699 | ASSERT(Verify(isolate, unoptimized, -1)); |
| 1700 | } |
| 1701 | |
| 1702 | |
yangguo@chromium.org | 4954674 | 2013-12-23 16:17:49 +0000 | [diff] [blame] | 1703 | void BackEdgeTable::AddStackCheck(Handle<Code> code, uint32_t pc_offset) { |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1704 | DisallowHeapAllocation no_gc; |
yangguo@chromium.org | 4954674 | 2013-12-23 16:17:49 +0000 | [diff] [blame] | 1705 | Isolate* isolate = code->GetIsolate(); |
| 1706 | Address pc = code->instruction_start() + pc_offset; |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1707 | Code* patch = isolate->builtins()->builtin(Builtins::kOsrAfterStackCheck); |
yangguo@chromium.org | 4954674 | 2013-12-23 16:17:49 +0000 | [diff] [blame] | 1708 | PatchAt(*code, pc, OSR_AFTER_STACK_CHECK, patch); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | |
yangguo@chromium.org | 4954674 | 2013-12-23 16:17:49 +0000 | [diff] [blame] | 1712 | void BackEdgeTable::RemoveStackCheck(Handle<Code> code, uint32_t pc_offset) { |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1713 | DisallowHeapAllocation no_gc; |
yangguo@chromium.org | 4954674 | 2013-12-23 16:17:49 +0000 | [diff] [blame] | 1714 | Isolate* isolate = code->GetIsolate(); |
| 1715 | Address pc = code->instruction_start() + pc_offset; |
| 1716 | |
| 1717 | if (OSR_AFTER_STACK_CHECK == GetBackEdgeState(isolate, *code, pc)) { |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1718 | Code* patch = isolate->builtins()->builtin(Builtins::kOnStackReplacement); |
yangguo@chromium.org | 4954674 | 2013-12-23 16:17:49 +0000 | [diff] [blame] | 1719 | PatchAt(*code, pc, ON_STACK_REPLACEMENT, patch); |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1724 | #ifdef DEBUG |
| 1725 | bool BackEdgeTable::Verify(Isolate* isolate, |
| 1726 | Code* unoptimized, |
| 1727 | int loop_nesting_level) { |
| 1728 | DisallowHeapAllocation no_gc; |
| 1729 | BackEdgeTable back_edges(unoptimized, &no_gc); |
| 1730 | for (uint32_t i = 0; i < back_edges.length(); i++) { |
| 1731 | uint32_t loop_depth = back_edges.loop_depth(i); |
| 1732 | CHECK_LE(static_cast<int>(loop_depth), Code::kMaxLoopNestingMarker); |
| 1733 | // Assert that all back edges for shallower loops (and only those) |
| 1734 | // have already been patched. |
| 1735 | CHECK_EQ((static_cast<int>(loop_depth) <= loop_nesting_level), |
| 1736 | GetBackEdgeState(isolate, |
| 1737 | unoptimized, |
| 1738 | back_edges.pc(i)) != INTERRUPT); |
| 1739 | } |
| 1740 | return true; |
| 1741 | } |
| 1742 | #endif // DEBUG |
| 1743 | |
| 1744 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 1745 | #undef __ |
| 1746 | |
| 1747 | |
| 1748 | } } // namespace v8::internal |