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