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