Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include "v8.h" |
| 29 | |
| 30 | #include "codegen-inl.h" |
| 31 | #include "compiler.h" |
| 32 | #include "full-codegen.h" |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 33 | #include "macro-assembler.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 34 | #include "scopes.h" |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 35 | #include "stub-cache.h" |
| 36 | #include "debug.h" |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 37 | #include "liveedit.h" |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 38 | |
| 39 | namespace v8 { |
| 40 | namespace internal { |
| 41 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 42 | void BreakableStatementChecker::Check(Statement* stmt) { |
| 43 | Visit(stmt); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | void BreakableStatementChecker::Check(Expression* expr) { |
| 48 | Visit(expr); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | void BreakableStatementChecker::VisitDeclaration(Declaration* decl) { |
| 53 | } |
| 54 | |
| 55 | |
| 56 | void BreakableStatementChecker::VisitBlock(Block* stmt) { |
| 57 | } |
| 58 | |
| 59 | |
| 60 | void BreakableStatementChecker::VisitExpressionStatement( |
| 61 | ExpressionStatement* stmt) { |
| 62 | // Check if expression is breakable. |
| 63 | Visit(stmt->expression()); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) { |
| 68 | } |
| 69 | |
| 70 | |
| 71 | void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) { |
| 72 | // If the condition is breakable the if statement is breakable. |
| 73 | Visit(stmt->condition()); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void BreakableStatementChecker::VisitContinueStatement( |
| 78 | ContinueStatement* stmt) { |
| 79 | } |
| 80 | |
| 81 | |
| 82 | void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) { |
| 83 | } |
| 84 | |
| 85 | |
| 86 | void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) { |
| 87 | // Return is breakable if the expression is. |
| 88 | Visit(stmt->expression()); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | void BreakableStatementChecker::VisitWithEnterStatement( |
| 93 | WithEnterStatement* stmt) { |
| 94 | Visit(stmt->expression()); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void BreakableStatementChecker::VisitWithExitStatement( |
| 99 | WithExitStatement* stmt) { |
| 100 | } |
| 101 | |
| 102 | |
| 103 | void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) { |
| 104 | // Switch statements breakable if the tag expression is. |
| 105 | Visit(stmt->tag()); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 110 | // Mark do while as breakable to avoid adding a break slot in front of it. |
| 111 | is_breakable_ = true; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) { |
| 116 | // Mark while statements breakable if the condition expression is. |
| 117 | Visit(stmt->cond()); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) { |
| 122 | // Mark for statements breakable if the condition expression is. |
| 123 | if (stmt->cond() != NULL) { |
| 124 | Visit(stmt->cond()); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) { |
| 130 | // Mark for in statements breakable if the enumerable expression is. |
| 131 | Visit(stmt->enumerable()); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | void BreakableStatementChecker::VisitTryCatchStatement( |
| 136 | TryCatchStatement* stmt) { |
| 137 | // Mark try catch as breakable to avoid adding a break slot in front of it. |
| 138 | is_breakable_ = true; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void BreakableStatementChecker::VisitTryFinallyStatement( |
| 143 | TryFinallyStatement* stmt) { |
| 144 | // Mark try finally as breakable to avoid adding a break slot in front of it. |
| 145 | is_breakable_ = true; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | void BreakableStatementChecker::VisitDebuggerStatement( |
| 150 | DebuggerStatement* stmt) { |
| 151 | // The debugger statement is breakable. |
| 152 | is_breakable_ = true; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void BreakableStatementChecker::VisitSharedFunctionInfoLiteral( |
| 161 | SharedFunctionInfoLiteral* expr) { |
| 162 | } |
| 163 | |
| 164 | |
| 165 | void BreakableStatementChecker::VisitConditional(Conditional* expr) { |
| 166 | } |
| 167 | |
| 168 | |
| 169 | void BreakableStatementChecker::VisitSlot(Slot* expr) { |
| 170 | } |
| 171 | |
| 172 | |
| 173 | void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) { |
| 174 | } |
| 175 | |
| 176 | |
| 177 | void BreakableStatementChecker::VisitLiteral(Literal* expr) { |
| 178 | } |
| 179 | |
| 180 | |
| 181 | void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 182 | } |
| 183 | |
| 184 | |
| 185 | void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) { |
| 186 | } |
| 187 | |
| 188 | |
| 189 | void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) { |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void BreakableStatementChecker::VisitCatchExtensionObject( |
| 194 | CatchExtensionObject* expr) { |
| 195 | } |
| 196 | |
| 197 | |
| 198 | void BreakableStatementChecker::VisitAssignment(Assignment* expr) { |
| 199 | // If assigning to a property (including a global property) the assignment is |
| 200 | // breakable. |
| 201 | Variable* var = expr->target()->AsVariableProxy()->AsVariable(); |
| 202 | Property* prop = expr->target()->AsProperty(); |
| 203 | if (prop != NULL || (var != NULL && var->is_global())) { |
| 204 | is_breakable_ = true; |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | // Otherwise the assignment is breakable if the assigned value is. |
| 209 | Visit(expr->value()); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | void BreakableStatementChecker::VisitThrow(Throw* expr) { |
| 214 | // Throw is breakable if the expression is. |
| 215 | Visit(expr->exception()); |
| 216 | } |
| 217 | |
| 218 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 219 | void BreakableStatementChecker::VisitIncrementOperation( |
| 220 | IncrementOperation* expr) { |
| 221 | UNREACHABLE(); |
| 222 | } |
| 223 | |
| 224 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 225 | void BreakableStatementChecker::VisitProperty(Property* expr) { |
| 226 | // Property load is breakable. |
| 227 | is_breakable_ = true; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | void BreakableStatementChecker::VisitCall(Call* expr) { |
| 232 | // Function calls both through IC and call stub are breakable. |
| 233 | is_breakable_ = true; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | void BreakableStatementChecker::VisitCallNew(CallNew* expr) { |
| 238 | // Function calls through new are breakable. |
| 239 | is_breakable_ = true; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) { |
| 244 | } |
| 245 | |
| 246 | |
| 247 | void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) { |
| 248 | Visit(expr->expression()); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) { |
| 253 | Visit(expr->expression()); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) { |
| 258 | Visit(expr->left()); |
| 259 | Visit(expr->right()); |
| 260 | } |
| 261 | |
| 262 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 263 | void BreakableStatementChecker::VisitCompareToNull(CompareToNull* expr) { |
| 264 | Visit(expr->expression()); |
| 265 | } |
| 266 | |
| 267 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 268 | void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) { |
| 269 | Visit(expr->left()); |
| 270 | Visit(expr->right()); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) { |
| 275 | } |
| 276 | |
| 277 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 278 | #define __ ACCESS_MASM(masm()) |
| 279 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 280 | bool FullCodeGenerator::MakeCode(CompilationInfo* info) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 281 | Handle<Script> script = info->script(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 282 | if (!script->IsUndefined() && !script->source()->IsUndefined()) { |
| 283 | int len = String::cast(script->source())->length(); |
| 284 | Counters::total_full_codegen_source_size.Increment(len); |
| 285 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 286 | CodeGenerator::MakeCodePrologue(info); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 287 | const int kInitialBufferSize = 4 * KB; |
| 288 | MacroAssembler masm(NULL, kInitialBufferSize); |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 289 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 290 | FullCodeGenerator cgen(&masm); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 291 | cgen.Generate(info); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 292 | if (cgen.HasStackOverflow()) { |
| 293 | ASSERT(!Top::has_pending_exception()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 294 | return false; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 295 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 296 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 297 | Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 298 | Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info); |
| 299 | info->SetCode(code); // may be an empty handle. |
| 300 | return !code.is_null(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 304 | MemOperand FullCodeGenerator::ContextOperand(Register context, int index) { |
| 305 | return CodeGenerator::ContextOperand(context, index); |
| 306 | } |
| 307 | |
| 308 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 309 | int FullCodeGenerator::SlotOffset(Slot* slot) { |
| 310 | ASSERT(slot != NULL); |
| 311 | // Offset is negative because higher indexes are at lower addresses. |
| 312 | int offset = -slot->index() * kPointerSize; |
| 313 | // Adjust by a (parameter or local) base offset. |
| 314 | switch (slot->type()) { |
| 315 | case Slot::PARAMETER: |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 316 | offset += (scope()->num_parameters() + 1) * kPointerSize; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 317 | break; |
| 318 | case Slot::LOCAL: |
| 319 | offset += JavaScriptFrameConstants::kLocal0Offset; |
| 320 | break; |
| 321 | case Slot::CONTEXT: |
| 322 | case Slot::LOOKUP: |
| 323 | UNREACHABLE(); |
| 324 | } |
| 325 | return offset; |
| 326 | } |
| 327 | |
| 328 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 329 | bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 330 | // Inline smi case inside loops, but not division and modulo which |
| 331 | // are too complicated and take up too much space. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 332 | if (op == Token::DIV ||op == Token::MOD) return false; |
| 333 | if (FLAG_always_inline_smi_code) return true; |
| 334 | return loop_depth_ > 0; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 338 | void FullCodeGenerator::EffectContext::Plug(Register reg) const { |
| 339 | } |
| 340 | |
| 341 | |
| 342 | void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const { |
| 343 | // Move value into place. |
| 344 | __ Move(result_register(), reg); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | void FullCodeGenerator::StackValueContext::Plug(Register reg) const { |
| 349 | // Move value into place. |
| 350 | __ push(reg); |
| 351 | } |
| 352 | |
| 353 | |
| 354 | void FullCodeGenerator::TestContext::Plug(Register reg) const { |
| 355 | // For simplicity we always test the accumulator register. |
| 356 | __ Move(result_register(), reg); |
| 357 | codegen()->DoTest(true_label_, false_label_, fall_through_); |
| 358 | } |
| 359 | |
| 360 | |
| 361 | void FullCodeGenerator::EffectContext::PlugTOS() const { |
| 362 | __ Drop(1); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const { |
| 367 | __ pop(result_register()); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | void FullCodeGenerator::StackValueContext::PlugTOS() const { |
| 372 | } |
| 373 | |
| 374 | |
| 375 | void FullCodeGenerator::TestContext::PlugTOS() const { |
| 376 | // For simplicity we always test the accumulator register. |
| 377 | __ pop(result_register()); |
| 378 | codegen()->DoTest(true_label_, false_label_, fall_through_); |
| 379 | } |
| 380 | |
| 381 | |
| 382 | void FullCodeGenerator::EffectContext::PrepareTest( |
| 383 | Label* materialize_true, |
| 384 | Label* materialize_false, |
| 385 | Label** if_true, |
| 386 | Label** if_false, |
| 387 | Label** fall_through) const { |
| 388 | // In an effect context, the true and the false case branch to the |
| 389 | // same label. |
| 390 | *if_true = *if_false = *fall_through = materialize_true; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | void FullCodeGenerator::AccumulatorValueContext::PrepareTest( |
| 395 | Label* materialize_true, |
| 396 | Label* materialize_false, |
| 397 | Label** if_true, |
| 398 | Label** if_false, |
| 399 | Label** fall_through) const { |
| 400 | *if_true = *fall_through = materialize_true; |
| 401 | *if_false = materialize_false; |
| 402 | } |
| 403 | |
| 404 | |
| 405 | void FullCodeGenerator::StackValueContext::PrepareTest( |
| 406 | Label* materialize_true, |
| 407 | Label* materialize_false, |
| 408 | Label** if_true, |
| 409 | Label** if_false, |
| 410 | Label** fall_through) const { |
| 411 | *if_true = *fall_through = materialize_true; |
| 412 | *if_false = materialize_false; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | void FullCodeGenerator::TestContext::PrepareTest( |
| 417 | Label* materialize_true, |
| 418 | Label* materialize_false, |
| 419 | Label** if_true, |
| 420 | Label** if_false, |
| 421 | Label** fall_through) const { |
| 422 | *if_true = true_label_; |
| 423 | *if_false = false_label_; |
| 424 | *fall_through = fall_through_; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 428 | void FullCodeGenerator::VisitDeclarations( |
| 429 | ZoneList<Declaration*>* declarations) { |
| 430 | int length = declarations->length(); |
| 431 | int globals = 0; |
| 432 | for (int i = 0; i < length; i++) { |
| 433 | Declaration* decl = declarations->at(i); |
| 434 | Variable* var = decl->proxy()->var(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 435 | Slot* slot = var->AsSlot(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 436 | |
| 437 | // If it was not possible to allocate the variable at compile |
| 438 | // time, we need to "declare" it at runtime to make sure it |
| 439 | // actually exists in the local context. |
| 440 | if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) { |
| 441 | VisitDeclaration(decl); |
| 442 | } else { |
| 443 | // Count global variables and functions for later processing |
| 444 | globals++; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | // Compute array of global variable and function declarations. |
| 449 | // Do nothing in case of no declared global functions or variables. |
| 450 | if (globals > 0) { |
| 451 | Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED); |
| 452 | for (int j = 0, i = 0; i < length; i++) { |
| 453 | Declaration* decl = declarations->at(i); |
| 454 | Variable* var = decl->proxy()->var(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 455 | Slot* slot = var->AsSlot(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 456 | |
| 457 | if ((slot == NULL || slot->type() != Slot::LOOKUP) && var->is_global()) { |
| 458 | array->set(j++, *(var->name())); |
| 459 | if (decl->fun() == NULL) { |
| 460 | if (var->mode() == Variable::CONST) { |
| 461 | // In case this is const property use the hole. |
| 462 | array->set_the_hole(j++); |
| 463 | } else { |
| 464 | array->set_undefined(j++); |
| 465 | } |
| 466 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 467 | Handle<SharedFunctionInfo> function = |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 468 | Compiler::BuildFunctionInfo(decl->fun(), script()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 469 | // Check for stack-overflow exception. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 470 | if (function.is_null()) { |
| 471 | SetStackOverflow(); |
| 472 | return; |
| 473 | } |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 474 | array->set(j++, *function); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | // Invoke the platform-dependent code generator to do the actual |
| 479 | // declaration the global variables and functions. |
| 480 | DeclareGlobals(array); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | |
| 485 | void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) { |
| 486 | if (FLAG_debug_info) { |
| 487 | CodeGenerator::RecordPositions(masm_, fun->start_position()); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | |
| 492 | void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) { |
| 493 | if (FLAG_debug_info) { |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 494 | CodeGenerator::RecordPositions(masm_, fun->end_position() - 1); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | |
| 499 | void FullCodeGenerator::SetStatementPosition(Statement* stmt) { |
| 500 | if (FLAG_debug_info) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 501 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 502 | if (!Debugger::IsDebuggerActive()) { |
| 503 | CodeGenerator::RecordPositions(masm_, stmt->statement_pos()); |
| 504 | } else { |
| 505 | // Check if the statement will be breakable without adding a debug break |
| 506 | // slot. |
| 507 | BreakableStatementChecker checker; |
| 508 | checker.Check(stmt); |
| 509 | // Record the statement position right here if the statement is not |
| 510 | // breakable. For breakable statements the actual recording of the |
| 511 | // position will be postponed to the breakable code (typically an IC). |
| 512 | bool position_recorded = CodeGenerator::RecordPositions( |
| 513 | masm_, stmt->statement_pos(), !checker.is_breakable()); |
| 514 | // If the position recording did record a new position generate a debug |
| 515 | // break slot to make the statement breakable. |
| 516 | if (position_recorded) { |
| 517 | Debug::GenerateSlot(masm_); |
| 518 | } |
| 519 | } |
| 520 | #else |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 521 | CodeGenerator::RecordPositions(masm_, stmt->statement_pos()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 522 | #endif |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | |
| 527 | void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) { |
| 528 | if (FLAG_debug_info) { |
| 529 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 530 | if (!Debugger::IsDebuggerActive()) { |
| 531 | CodeGenerator::RecordPositions(masm_, pos); |
| 532 | } else { |
| 533 | // Check if the expression will be breakable without adding a debug break |
| 534 | // slot. |
| 535 | BreakableStatementChecker checker; |
| 536 | checker.Check(expr); |
| 537 | // Record a statement position right here if the expression is not |
| 538 | // breakable. For breakable expressions the actual recording of the |
| 539 | // position will be postponed to the breakable code (typically an IC). |
| 540 | // NOTE this will record a statement position for something which might |
| 541 | // not be a statement. As stepping in the debugger will only stop at |
| 542 | // statement positions this is used for e.g. the condition expression of |
| 543 | // a do while loop. |
| 544 | bool position_recorded = CodeGenerator::RecordPositions( |
| 545 | masm_, pos, !checker.is_breakable()); |
| 546 | // If the position recording did record a new position generate a debug |
| 547 | // break slot to make the statement breakable. |
| 548 | if (position_recorded) { |
| 549 | Debug::GenerateSlot(masm_); |
| 550 | } |
| 551 | } |
| 552 | #else |
| 553 | CodeGenerator::RecordPositions(masm_, pos); |
| 554 | #endif |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
| 558 | |
| 559 | void FullCodeGenerator::SetStatementPosition(int pos) { |
| 560 | if (FLAG_debug_info) { |
| 561 | CodeGenerator::RecordPositions(masm_, pos); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame^] | 566 | void FullCodeGenerator::SetSourcePosition( |
| 567 | int pos, PositionRecordingType recording_type) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 568 | if (FLAG_debug_info && pos != RelocInfo::kNoPosition) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame^] | 569 | masm_->positions_recorder()->RecordPosition(pos, recording_type); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 574 | // Lookup table for code generators for special runtime calls which are |
| 575 | // generated inline. |
| 576 | #define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize) \ |
| 577 | &FullCodeGenerator::Emit##Name, |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 578 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 579 | const FullCodeGenerator::InlineFunctionGenerator |
| 580 | FullCodeGenerator::kInlineFunctionGenerators[] = { |
| 581 | INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 582 | INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS) |
| 583 | }; |
| 584 | #undef INLINE_FUNCTION_GENERATOR_ADDRESS |
| 585 | |
| 586 | |
| 587 | FullCodeGenerator::InlineFunctionGenerator |
| 588 | FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) { |
| 589 | return kInlineFunctionGenerators[ |
| 590 | static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction)]; |
| 591 | } |
| 592 | |
| 593 | |
| 594 | void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* node) { |
| 595 | ZoneList<Expression*>* args = node->arguments(); |
| 596 | Handle<String> name = node->name(); |
| 597 | Runtime::Function* function = node->function(); |
| 598 | ASSERT(function != NULL); |
| 599 | ASSERT(function->intrinsic_type == Runtime::INLINE); |
| 600 | InlineFunctionGenerator generator = |
| 601 | FindInlineFunctionGenerator(function->function_id); |
| 602 | ASSERT(generator != NULL); |
| 603 | ((*this).*(generator))(args); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | |
| 607 | void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) { |
| 608 | Comment cmnt(masm_, "[ BinaryOperation"); |
| 609 | Token::Value op = expr->op(); |
| 610 | Expression* left = expr->left(); |
| 611 | Expression* right = expr->right(); |
| 612 | |
| 613 | OverwriteMode mode = NO_OVERWRITE; |
| 614 | if (left->ResultOverwriteAllowed()) { |
| 615 | mode = OVERWRITE_LEFT; |
| 616 | } else if (right->ResultOverwriteAllowed()) { |
| 617 | mode = OVERWRITE_RIGHT; |
| 618 | } |
| 619 | |
| 620 | switch (op) { |
| 621 | case Token::COMMA: |
| 622 | VisitForEffect(left); |
| 623 | Visit(right); |
| 624 | break; |
| 625 | |
| 626 | case Token::OR: |
| 627 | case Token::AND: |
| 628 | EmitLogicalOperation(expr); |
| 629 | break; |
| 630 | |
| 631 | case Token::ADD: |
| 632 | case Token::SUB: |
| 633 | case Token::DIV: |
| 634 | case Token::MOD: |
| 635 | case Token::MUL: |
| 636 | case Token::BIT_OR: |
| 637 | case Token::BIT_AND: |
| 638 | case Token::BIT_XOR: |
| 639 | case Token::SHL: |
| 640 | case Token::SHR: |
| 641 | case Token::SAR: { |
| 642 | // Figure out if either of the operands is a constant. |
| 643 | ConstantOperand constant = ShouldInlineSmiCase(op) |
| 644 | ? GetConstantOperand(op, left, right) |
| 645 | : kNoConstants; |
| 646 | |
| 647 | // Load only the operands that we need to materialize. |
| 648 | if (constant == kNoConstants) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 649 | VisitForStackValue(left); |
| 650 | VisitForAccumulatorValue(right); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 651 | } else if (constant == kRightConstant) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 652 | VisitForAccumulatorValue(left); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 653 | } else { |
| 654 | ASSERT(constant == kLeftConstant); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 655 | VisitForAccumulatorValue(right); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | SetSourcePosition(expr->position()); |
| 659 | if (ShouldInlineSmiCase(op)) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 660 | EmitInlineSmiBinaryOp(expr, op, mode, left, right, constant); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 661 | } else { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 662 | EmitBinaryOp(op, mode); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 663 | } |
| 664 | break; |
| 665 | } |
| 666 | |
| 667 | default: |
| 668 | UNREACHABLE(); |
| 669 | } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 673 | void FullCodeGenerator::EmitLogicalOperation(BinaryOperation* expr) { |
| 674 | Label eval_right, done; |
| 675 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 676 | context()->EmitLogicalLeft(expr, &eval_right, &done); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 677 | |
| 678 | __ bind(&eval_right); |
| 679 | Visit(expr->right()); |
| 680 | |
| 681 | __ bind(&done); |
| 682 | } |
| 683 | |
| 684 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 685 | void FullCodeGenerator::EffectContext::EmitLogicalLeft(BinaryOperation* expr, |
| 686 | Label* eval_right, |
| 687 | Label* done) const { |
| 688 | if (expr->op() == Token::OR) { |
| 689 | codegen()->VisitForControl(expr->left(), done, eval_right, eval_right); |
| 690 | } else { |
| 691 | ASSERT(expr->op() == Token::AND); |
| 692 | codegen()->VisitForControl(expr->left(), eval_right, done, eval_right); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 693 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 694 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 695 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 696 | |
| 697 | void FullCodeGenerator::AccumulatorValueContext::EmitLogicalLeft( |
| 698 | BinaryOperation* expr, |
| 699 | Label* eval_right, |
| 700 | Label* done) const { |
| 701 | codegen()->Visit(expr->left()); |
| 702 | // We want the value in the accumulator for the test, and on the stack in case |
| 703 | // we need it. |
| 704 | __ push(result_register()); |
| 705 | Label discard, restore; |
| 706 | if (expr->op() == Token::OR) { |
| 707 | codegen()->DoTest(&restore, &discard, &restore); |
| 708 | } else { |
| 709 | ASSERT(expr->op() == Token::AND); |
| 710 | codegen()->DoTest(&discard, &restore, &restore); |
| 711 | } |
| 712 | __ bind(&restore); |
| 713 | __ pop(result_register()); |
| 714 | __ jmp(done); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 715 | __ bind(&discard); |
| 716 | __ Drop(1); |
| 717 | } |
| 718 | |
| 719 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 720 | void FullCodeGenerator::StackValueContext::EmitLogicalLeft( |
| 721 | BinaryOperation* expr, |
| 722 | Label* eval_right, |
| 723 | Label* done) const { |
| 724 | codegen()->VisitForAccumulatorValue(expr->left()); |
| 725 | // We want the value in the accumulator for the test, and on the stack in case |
| 726 | // we need it. |
| 727 | __ push(result_register()); |
| 728 | Label discard; |
| 729 | if (expr->op() == Token::OR) { |
| 730 | codegen()->DoTest(done, &discard, &discard); |
| 731 | } else { |
| 732 | ASSERT(expr->op() == Token::AND); |
| 733 | codegen()->DoTest(&discard, done, &discard); |
| 734 | } |
| 735 | __ bind(&discard); |
| 736 | __ Drop(1); |
| 737 | } |
| 738 | |
| 739 | |
| 740 | void FullCodeGenerator::TestContext::EmitLogicalLeft(BinaryOperation* expr, |
| 741 | Label* eval_right, |
| 742 | Label* done) const { |
| 743 | if (expr->op() == Token::OR) { |
| 744 | codegen()->VisitForControl(expr->left(), |
| 745 | true_label_, eval_right, eval_right); |
| 746 | } else { |
| 747 | ASSERT(expr->op() == Token::AND); |
| 748 | codegen()->VisitForControl(expr->left(), |
| 749 | eval_right, false_label_, eval_right); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 754 | void FullCodeGenerator::VisitBlock(Block* stmt) { |
| 755 | Comment cmnt(masm_, "[ Block"); |
| 756 | Breakable nested_statement(this, stmt); |
| 757 | SetStatementPosition(stmt); |
| 758 | VisitStatements(stmt->statements()); |
| 759 | __ bind(nested_statement.break_target()); |
| 760 | } |
| 761 | |
| 762 | |
| 763 | void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) { |
| 764 | Comment cmnt(masm_, "[ ExpressionStatement"); |
| 765 | SetStatementPosition(stmt); |
| 766 | VisitForEffect(stmt->expression()); |
| 767 | } |
| 768 | |
| 769 | |
| 770 | void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) { |
| 771 | Comment cmnt(masm_, "[ EmptyStatement"); |
| 772 | SetStatementPosition(stmt); |
| 773 | } |
| 774 | |
| 775 | |
| 776 | void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) { |
| 777 | Comment cmnt(masm_, "[ IfStatement"); |
| 778 | SetStatementPosition(stmt); |
| 779 | Label then_part, else_part, done; |
| 780 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 781 | if (stmt->HasElseStatement()) { |
| 782 | VisitForControl(stmt->condition(), &then_part, &else_part, &then_part); |
| 783 | __ bind(&then_part); |
| 784 | Visit(stmt->then_statement()); |
| 785 | __ jmp(&done); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 786 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 787 | __ bind(&else_part); |
| 788 | Visit(stmt->else_statement()); |
| 789 | } else { |
| 790 | VisitForControl(stmt->condition(), &then_part, &done, &then_part); |
| 791 | __ bind(&then_part); |
| 792 | Visit(stmt->then_statement()); |
| 793 | } |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 794 | __ bind(&done); |
| 795 | } |
| 796 | |
| 797 | |
| 798 | void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) { |
| 799 | Comment cmnt(masm_, "[ ContinueStatement"); |
| 800 | SetStatementPosition(stmt); |
| 801 | NestedStatement* current = nesting_stack_; |
| 802 | int stack_depth = 0; |
| 803 | while (!current->IsContinueTarget(stmt->target())) { |
| 804 | stack_depth = current->Exit(stack_depth); |
| 805 | current = current->outer(); |
| 806 | } |
| 807 | __ Drop(stack_depth); |
| 808 | |
| 809 | Iteration* loop = current->AsIteration(); |
| 810 | __ jmp(loop->continue_target()); |
| 811 | } |
| 812 | |
| 813 | |
| 814 | void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) { |
| 815 | Comment cmnt(masm_, "[ BreakStatement"); |
| 816 | SetStatementPosition(stmt); |
| 817 | NestedStatement* current = nesting_stack_; |
| 818 | int stack_depth = 0; |
| 819 | while (!current->IsBreakTarget(stmt->target())) { |
| 820 | stack_depth = current->Exit(stack_depth); |
| 821 | current = current->outer(); |
| 822 | } |
| 823 | __ Drop(stack_depth); |
| 824 | |
| 825 | Breakable* target = current->AsBreakable(); |
| 826 | __ jmp(target->break_target()); |
| 827 | } |
| 828 | |
| 829 | |
| 830 | void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) { |
| 831 | Comment cmnt(masm_, "[ ReturnStatement"); |
| 832 | SetStatementPosition(stmt); |
| 833 | Expression* expr = stmt->expression(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 834 | VisitForAccumulatorValue(expr); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 835 | |
| 836 | // Exit all nested statements. |
| 837 | NestedStatement* current = nesting_stack_; |
| 838 | int stack_depth = 0; |
| 839 | while (current != NULL) { |
| 840 | stack_depth = current->Exit(stack_depth); |
| 841 | current = current->outer(); |
| 842 | } |
| 843 | __ Drop(stack_depth); |
| 844 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 845 | EmitReturnSequence(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | |
| 849 | void FullCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) { |
| 850 | Comment cmnt(masm_, "[ WithEnterStatement"); |
| 851 | SetStatementPosition(stmt); |
| 852 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 853 | VisitForStackValue(stmt->expression()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 854 | if (stmt->is_catch_block()) { |
| 855 | __ CallRuntime(Runtime::kPushCatchContext, 1); |
| 856 | } else { |
| 857 | __ CallRuntime(Runtime::kPushContext, 1); |
| 858 | } |
| 859 | // Both runtime calls return the new context in both the context and the |
| 860 | // result registers. |
| 861 | |
| 862 | // Update local stack frame context field. |
| 863 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
| 864 | } |
| 865 | |
| 866 | |
| 867 | void FullCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) { |
| 868 | Comment cmnt(masm_, "[ WithExitStatement"); |
| 869 | SetStatementPosition(stmt); |
| 870 | |
| 871 | // Pop context. |
| 872 | LoadContextField(context_register(), Context::PREVIOUS_INDEX); |
| 873 | // Update local stack frame context field. |
| 874 | StoreToFrameField(StandardFrameConstants::kContextOffset, context_register()); |
| 875 | } |
| 876 | |
| 877 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 878 | void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 879 | Comment cmnt(masm_, "[ DoWhileStatement"); |
| 880 | SetStatementPosition(stmt); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 881 | Label body, stack_limit_hit, stack_check_success, done; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 882 | |
| 883 | Iteration loop_statement(this, stmt); |
| 884 | increment_loop_depth(); |
| 885 | |
| 886 | __ bind(&body); |
| 887 | Visit(stmt->body()); |
| 888 | |
| 889 | // Check stack before looping. |
| 890 | __ StackLimitCheck(&stack_limit_hit); |
| 891 | __ bind(&stack_check_success); |
| 892 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 893 | // Record the position of the do while condition and make sure it is |
| 894 | // possible to break on the condition. |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 895 | __ bind(loop_statement.continue_target()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 896 | SetExpressionPosition(stmt->cond(), stmt->condition_position()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 897 | VisitForControl(stmt->cond(), |
| 898 | &body, |
| 899 | loop_statement.break_target(), |
| 900 | loop_statement.break_target()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 901 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 902 | __ bind(loop_statement.break_target()); |
| 903 | __ jmp(&done); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 904 | |
| 905 | __ bind(&stack_limit_hit); |
| 906 | StackCheckStub stack_stub; |
| 907 | __ CallStub(&stack_stub); |
| 908 | __ jmp(&stack_check_success); |
| 909 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 910 | __ bind(&done); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 911 | decrement_loop_depth(); |
| 912 | } |
| 913 | |
| 914 | |
| 915 | void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) { |
| 916 | Comment cmnt(masm_, "[ WhileStatement"); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 917 | Label body, stack_limit_hit, stack_check_success, done; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 918 | |
| 919 | Iteration loop_statement(this, stmt); |
| 920 | increment_loop_depth(); |
| 921 | |
| 922 | // Emit the test at the bottom of the loop. |
| 923 | __ jmp(loop_statement.continue_target()); |
| 924 | |
| 925 | __ bind(&body); |
| 926 | Visit(stmt->body()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 927 | __ bind(loop_statement.continue_target()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 928 | |
| 929 | // Emit the statement position here as this is where the while |
| 930 | // statement code starts. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 931 | SetStatementPosition(stmt); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 932 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 933 | // Check stack before looping. |
| 934 | __ StackLimitCheck(&stack_limit_hit); |
| 935 | __ bind(&stack_check_success); |
| 936 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 937 | VisitForControl(stmt->cond(), |
| 938 | &body, |
| 939 | loop_statement.break_target(), |
| 940 | loop_statement.break_target()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 941 | |
| 942 | __ bind(loop_statement.break_target()); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 943 | __ jmp(&done); |
| 944 | |
| 945 | __ bind(&stack_limit_hit); |
| 946 | StackCheckStub stack_stub; |
| 947 | __ CallStub(&stack_stub); |
| 948 | __ jmp(&stack_check_success); |
| 949 | |
| 950 | __ bind(&done); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 951 | decrement_loop_depth(); |
| 952 | } |
| 953 | |
| 954 | |
| 955 | void FullCodeGenerator::VisitForStatement(ForStatement* stmt) { |
| 956 | Comment cmnt(masm_, "[ ForStatement"); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 957 | Label test, body, stack_limit_hit, stack_check_success; |
| 958 | |
| 959 | Iteration loop_statement(this, stmt); |
| 960 | if (stmt->init() != NULL) { |
| 961 | Visit(stmt->init()); |
| 962 | } |
| 963 | |
| 964 | increment_loop_depth(); |
| 965 | // Emit the test at the bottom of the loop (even if empty). |
| 966 | __ jmp(&test); |
| 967 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 968 | __ bind(&stack_limit_hit); |
| 969 | StackCheckStub stack_stub; |
| 970 | __ CallStub(&stack_stub); |
| 971 | __ jmp(&stack_check_success); |
| 972 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 973 | __ bind(&body); |
| 974 | Visit(stmt->body()); |
| 975 | |
| 976 | __ bind(loop_statement.continue_target()); |
| 977 | |
| 978 | SetStatementPosition(stmt); |
| 979 | if (stmt->next() != NULL) { |
| 980 | Visit(stmt->next()); |
| 981 | } |
| 982 | |
| 983 | __ bind(&test); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 984 | // Emit the statement position here as this is where the for |
| 985 | // statement code starts. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 986 | SetStatementPosition(stmt); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 987 | |
| 988 | // Check stack before looping. |
| 989 | __ StackLimitCheck(&stack_limit_hit); |
| 990 | __ bind(&stack_check_success); |
| 991 | |
| 992 | if (stmt->cond() != NULL) { |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 993 | VisitForControl(stmt->cond(), |
| 994 | &body, |
| 995 | loop_statement.break_target(), |
| 996 | loop_statement.break_target()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 997 | } else { |
| 998 | __ jmp(&body); |
| 999 | } |
| 1000 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1001 | __ bind(loop_statement.break_target()); |
| 1002 | decrement_loop_depth(); |
| 1003 | } |
| 1004 | |
| 1005 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1006 | void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 1007 | Comment cmnt(masm_, "[ TryCatchStatement"); |
| 1008 | SetStatementPosition(stmt); |
| 1009 | // The try block adds a handler to the exception handler chain |
| 1010 | // before entering, and removes it again when exiting normally. |
| 1011 | // If an exception is thrown during execution of the try block, |
| 1012 | // control is passed to the handler, which also consumes the handler. |
| 1013 | // At this point, the exception is in a register, and store it in |
| 1014 | // the temporary local variable (prints as ".catch-var") before |
| 1015 | // executing the catch block. The catch block has been rewritten |
| 1016 | // to introduce a new scope to bind the catch variable and to remove |
| 1017 | // that scope again afterwards. |
| 1018 | |
| 1019 | Label try_handler_setup, catch_entry, done; |
| 1020 | __ Call(&try_handler_setup); |
| 1021 | // Try handler code, exception in result register. |
| 1022 | |
| 1023 | // Store exception in local .catch variable before executing catch block. |
| 1024 | { |
| 1025 | // The catch variable is *always* a variable proxy for a local variable. |
| 1026 | Variable* catch_var = stmt->catch_var()->AsVariableProxy()->AsVariable(); |
| 1027 | ASSERT_NOT_NULL(catch_var); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1028 | Slot* variable_slot = catch_var->AsSlot(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1029 | ASSERT_NOT_NULL(variable_slot); |
| 1030 | ASSERT_EQ(Slot::LOCAL, variable_slot->type()); |
| 1031 | StoreToFrameField(SlotOffset(variable_slot), result_register()); |
| 1032 | } |
| 1033 | |
| 1034 | Visit(stmt->catch_block()); |
| 1035 | __ jmp(&done); |
| 1036 | |
| 1037 | // Try block code. Sets up the exception handler chain. |
| 1038 | __ bind(&try_handler_setup); |
| 1039 | { |
| 1040 | TryCatch try_block(this, &catch_entry); |
| 1041 | __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER); |
| 1042 | Visit(stmt->try_block()); |
| 1043 | __ PopTryHandler(); |
| 1044 | } |
| 1045 | __ bind(&done); |
| 1046 | } |
| 1047 | |
| 1048 | |
| 1049 | void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
| 1050 | Comment cmnt(masm_, "[ TryFinallyStatement"); |
| 1051 | SetStatementPosition(stmt); |
| 1052 | // Try finally is compiled by setting up a try-handler on the stack while |
| 1053 | // executing the try body, and removing it again afterwards. |
| 1054 | // |
| 1055 | // The try-finally construct can enter the finally block in three ways: |
| 1056 | // 1. By exiting the try-block normally. This removes the try-handler and |
| 1057 | // calls the finally block code before continuing. |
| 1058 | // 2. By exiting the try-block with a function-local control flow transfer |
| 1059 | // (break/continue/return). The site of the, e.g., break removes the |
| 1060 | // try handler and calls the finally block code before continuing |
| 1061 | // its outward control transfer. |
| 1062 | // 3. by exiting the try-block with a thrown exception. |
| 1063 | // This can happen in nested function calls. It traverses the try-handler |
| 1064 | // chain and consumes the try-handler entry before jumping to the |
| 1065 | // handler code. The handler code then calls the finally-block before |
| 1066 | // rethrowing the exception. |
| 1067 | // |
| 1068 | // The finally block must assume a return address on top of the stack |
| 1069 | // (or in the link register on ARM chips) and a value (return value or |
| 1070 | // exception) in the result register (rax/eax/r0), both of which must |
| 1071 | // be preserved. The return address isn't GC-safe, so it should be |
| 1072 | // cooked before GC. |
| 1073 | Label finally_entry; |
| 1074 | Label try_handler_setup; |
| 1075 | |
| 1076 | // Setup the try-handler chain. Use a call to |
| 1077 | // Jump to try-handler setup and try-block code. Use call to put try-handler |
| 1078 | // address on stack. |
| 1079 | __ Call(&try_handler_setup); |
| 1080 | // Try handler code. Return address of call is pushed on handler stack. |
| 1081 | { |
| 1082 | // This code is only executed during stack-handler traversal when an |
| 1083 | // exception is thrown. The execption is in the result register, which |
| 1084 | // is retained by the finally block. |
| 1085 | // Call the finally block and then rethrow the exception. |
| 1086 | __ Call(&finally_entry); |
| 1087 | __ push(result_register()); |
| 1088 | __ CallRuntime(Runtime::kReThrow, 1); |
| 1089 | } |
| 1090 | |
| 1091 | __ bind(&finally_entry); |
| 1092 | { |
| 1093 | // Finally block implementation. |
| 1094 | Finally finally_block(this); |
| 1095 | EnterFinallyBlock(); |
| 1096 | Visit(stmt->finally_block()); |
| 1097 | ExitFinallyBlock(); // Return to the calling code. |
| 1098 | } |
| 1099 | |
| 1100 | __ bind(&try_handler_setup); |
| 1101 | { |
| 1102 | // Setup try handler (stack pointer registers). |
| 1103 | TryFinally try_block(this, &finally_entry); |
| 1104 | __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER); |
| 1105 | Visit(stmt->try_block()); |
| 1106 | __ PopTryHandler(); |
| 1107 | } |
| 1108 | // Execute the finally block on the way out. |
| 1109 | __ Call(&finally_entry); |
| 1110 | } |
| 1111 | |
| 1112 | |
| 1113 | void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) { |
| 1114 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1115 | Comment cmnt(masm_, "[ DebuggerStatement"); |
| 1116 | SetStatementPosition(stmt); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1117 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1118 | __ DebugBreak(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1119 | // Ignore the return value. |
| 1120 | #endif |
| 1121 | } |
| 1122 | |
| 1123 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1124 | void FullCodeGenerator::VisitConditional(Conditional* expr) { |
| 1125 | Comment cmnt(masm_, "[ Conditional"); |
| 1126 | Label true_case, false_case, done; |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1127 | VisitForControl(expr->condition(), &true_case, &false_case, &true_case); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1128 | |
| 1129 | __ bind(&true_case); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1130 | SetExpressionPosition(expr->then_expression(), |
| 1131 | expr->then_expression_position()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1132 | if (context()->IsTest()) { |
| 1133 | const TestContext* for_test = TestContext::cast(context()); |
| 1134 | VisitForControl(expr->then_expression(), |
| 1135 | for_test->true_label(), |
| 1136 | for_test->false_label(), |
| 1137 | NULL); |
| 1138 | } else { |
| 1139 | Visit(expr->then_expression()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1140 | __ jmp(&done); |
| 1141 | } |
| 1142 | |
| 1143 | __ bind(&false_case); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1144 | SetExpressionPosition(expr->else_expression(), |
| 1145 | expr->else_expression_position()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1146 | Visit(expr->else_expression()); |
| 1147 | // If control flow falls through Visit, merge it with true case here. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1148 | if (!context()->IsTest()) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1149 | __ bind(&done); |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | |
| 1154 | void FullCodeGenerator::VisitSlot(Slot* expr) { |
| 1155 | // Slots do not appear directly in the AST. |
| 1156 | UNREACHABLE(); |
| 1157 | } |
| 1158 | |
| 1159 | |
| 1160 | void FullCodeGenerator::VisitLiteral(Literal* expr) { |
| 1161 | Comment cmnt(masm_, "[ Literal"); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1162 | context()->Plug(expr->handle()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1166 | void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 1167 | Comment cmnt(masm_, "[ FunctionLiteral"); |
| 1168 | |
| 1169 | // Build the function boilerplate and instantiate it. |
| 1170 | Handle<SharedFunctionInfo> function_info = |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1171 | Compiler::BuildFunctionInfo(expr, script()); |
| 1172 | if (function_info.is_null()) { |
| 1173 | SetStackOverflow(); |
| 1174 | return; |
| 1175 | } |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1176 | EmitNewClosure(function_info); |
| 1177 | } |
| 1178 | |
| 1179 | |
| 1180 | void FullCodeGenerator::VisitSharedFunctionInfoLiteral( |
| 1181 | SharedFunctionInfoLiteral* expr) { |
| 1182 | Comment cmnt(masm_, "[ SharedFunctionInfoLiteral"); |
| 1183 | EmitNewClosure(expr->shared_function_info()); |
| 1184 | } |
| 1185 | |
| 1186 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1187 | void FullCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) { |
| 1188 | // Call runtime routine to allocate the catch extension object and |
| 1189 | // assign the exception value to the catch variable. |
| 1190 | Comment cmnt(masm_, "[ CatchExtensionObject"); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1191 | VisitForStackValue(expr->key()); |
| 1192 | VisitForStackValue(expr->value()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1193 | // Create catch extension object. |
| 1194 | __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1195 | context()->Plug(result_register()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | |
| 1199 | void FullCodeGenerator::VisitThrow(Throw* expr) { |
| 1200 | Comment cmnt(masm_, "[ Throw"); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1201 | VisitForStackValue(expr->exception()); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1202 | __ CallRuntime(Runtime::kThrow, 1); |
| 1203 | // Never returns here. |
| 1204 | } |
| 1205 | |
| 1206 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1207 | void FullCodeGenerator::VisitIncrementOperation(IncrementOperation* expr) { |
| 1208 | UNREACHABLE(); |
| 1209 | } |
| 1210 | |
| 1211 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1212 | int FullCodeGenerator::TryFinally::Exit(int stack_depth) { |
| 1213 | // The macros used here must preserve the result register. |
| 1214 | __ Drop(stack_depth); |
| 1215 | __ PopTryHandler(); |
| 1216 | __ Call(finally_entry_); |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | int FullCodeGenerator::TryCatch::Exit(int stack_depth) { |
| 1222 | // The macros used here must preserve the result register. |
| 1223 | __ Drop(stack_depth); |
| 1224 | __ PopTryHandler(); |
| 1225 | return 0; |
| 1226 | } |
| 1227 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1228 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 1229 | #undef __ |
| 1230 | |
| 1231 | |
| 1232 | } } // namespace v8::internal |