full-codegen.cc 41.5 KB
Newer Older
1
// Copyright 2010 the V8 project authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "v8.h"

#include "codegen-inl.h"
31
#include "compiler.h"
32
#include "debug.h"
33
#include "full-codegen.h"
34
#include "liveedit.h"
35
#include "macro-assembler.h"
36
#include "prettyprinter.h"
37
#include "scopes.h"
38
#include "stub-cache.h"
39 40 41 42

namespace v8 {
namespace internal {

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
void BreakableStatementChecker::Check(Statement* stmt) {
  Visit(stmt);
}


void BreakableStatementChecker::Check(Expression* expr) {
  Visit(expr);
}


void BreakableStatementChecker::VisitDeclaration(Declaration* decl) {
}


void BreakableStatementChecker::VisitBlock(Block* stmt) {
}


void BreakableStatementChecker::VisitExpressionStatement(
    ExpressionStatement* stmt) {
  // Check if expression is breakable.
  Visit(stmt->expression());
}


void BreakableStatementChecker::VisitEmptyStatement(EmptyStatement* stmt) {
}


void BreakableStatementChecker::VisitIfStatement(IfStatement* stmt) {
  // If the condition is breakable the if statement is breakable.
  Visit(stmt->condition());
}


void BreakableStatementChecker::VisitContinueStatement(
    ContinueStatement* stmt) {
}


void BreakableStatementChecker::VisitBreakStatement(BreakStatement* stmt) {
}


void BreakableStatementChecker::VisitReturnStatement(ReturnStatement* stmt) {
  // Return is breakable if the expression is.
  Visit(stmt->expression());
}


void BreakableStatementChecker::VisitWithEnterStatement(
    WithEnterStatement* stmt) {
  Visit(stmt->expression());
}


void BreakableStatementChecker::VisitWithExitStatement(
    WithExitStatement* stmt) {
}


void BreakableStatementChecker::VisitSwitchStatement(SwitchStatement* stmt) {
  // Switch statements breakable if the tag expression is.
  Visit(stmt->tag());
}


void BreakableStatementChecker::VisitDoWhileStatement(DoWhileStatement* stmt) {
  // Mark do while as breakable to avoid adding a break slot in front of it.
  is_breakable_ = true;
}


void BreakableStatementChecker::VisitWhileStatement(WhileStatement* stmt) {
  // Mark while statements breakable if the condition expression is.
  Visit(stmt->cond());
}


void BreakableStatementChecker::VisitForStatement(ForStatement* stmt) {
  // Mark for statements breakable if the condition expression is.
  if (stmt->cond() != NULL) {
    Visit(stmt->cond());
  }
}


void BreakableStatementChecker::VisitForInStatement(ForInStatement* stmt) {
  // Mark for in statements breakable if the enumerable expression is.
  Visit(stmt->enumerable());
}


void BreakableStatementChecker::VisitTryCatchStatement(
    TryCatchStatement* stmt) {
  // Mark try catch as breakable to avoid adding a break slot in front of it.
  is_breakable_ = true;
}


void BreakableStatementChecker::VisitTryFinallyStatement(
    TryFinallyStatement* stmt) {
  // Mark try finally as breakable to avoid adding a break slot in front of it.
  is_breakable_ = true;
}


void BreakableStatementChecker::VisitDebuggerStatement(
    DebuggerStatement* stmt) {
  // The debugger statement is breakable.
  is_breakable_ = true;
}


void BreakableStatementChecker::VisitFunctionLiteral(FunctionLiteral* expr) {
}


void BreakableStatementChecker::VisitSharedFunctionInfoLiteral(
    SharedFunctionInfoLiteral* expr) {
}


void BreakableStatementChecker::VisitConditional(Conditional* expr) {
}


void BreakableStatementChecker::VisitVariableProxy(VariableProxy* expr) {
}


void BreakableStatementChecker::VisitLiteral(Literal* expr) {
}


void BreakableStatementChecker::VisitRegExpLiteral(RegExpLiteral* expr) {
}


void BreakableStatementChecker::VisitObjectLiteral(ObjectLiteral* expr) {
}


void BreakableStatementChecker::VisitArrayLiteral(ArrayLiteral* expr) {
}


void BreakableStatementChecker::VisitCatchExtensionObject(
    CatchExtensionObject* expr) {
}


void BreakableStatementChecker::VisitAssignment(Assignment* expr) {
  // If assigning to a property (including a global property) the assignment is
  // breakable.
  Variable* var = expr->target()->AsVariableProxy()->AsVariable();
  Property* prop = expr->target()->AsProperty();
  if (prop != NULL || (var != NULL && var->is_global())) {
    is_breakable_ = true;
    return;
  }

  // Otherwise the assignment is breakable if the assigned value is.
  Visit(expr->value());
}


void BreakableStatementChecker::VisitThrow(Throw* expr) {
  // Throw is breakable if the expression is.
  Visit(expr->exception());
}


216 217 218 219 220 221
void BreakableStatementChecker::VisitIncrementOperation(
    IncrementOperation* expr) {
  UNREACHABLE();
}


222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
void BreakableStatementChecker::VisitProperty(Property* expr) {
  // Property load is breakable.
  is_breakable_ = true;
}


void BreakableStatementChecker::VisitCall(Call* expr) {
  // Function calls both through IC and call stub are breakable.
  is_breakable_ = true;
}


void BreakableStatementChecker::VisitCallNew(CallNew* expr) {
  // Function calls through new are breakable.
  is_breakable_ = true;
}


void BreakableStatementChecker::VisitCallRuntime(CallRuntime* expr) {
}


void BreakableStatementChecker::VisitUnaryOperation(UnaryOperation* expr) {
  Visit(expr->expression());
}


void BreakableStatementChecker::VisitCountOperation(CountOperation* expr) {
  Visit(expr->expression());
}


void BreakableStatementChecker::VisitBinaryOperation(BinaryOperation* expr) {
  Visit(expr->left());
  Visit(expr->right());
}


260 261 262 263 264
void BreakableStatementChecker::VisitCompareToNull(CompareToNull* expr) {
  Visit(expr->expression());
}


265 266 267 268 269 270 271 272 273 274
void BreakableStatementChecker::VisitCompareOperation(CompareOperation* expr) {
  Visit(expr->left());
  Visit(expr->right());
}


void BreakableStatementChecker::VisitThisFunction(ThisFunction* expr) {
}


275
#define __ ACCESS_MASM(masm())
276

277
bool FullCodeGenerator::MakeCode(CompilationInfo* info) {
278
  Handle<Script> script = info->script();
279 280 281 282
  if (!script->IsUndefined() && !script->source()->IsUndefined()) {
    int len = String::cast(script->source())->length();
    Counters::total_full_codegen_source_size.Increment(len);
  }
283 284 285
  if (FLAG_trace_codegen) {
    PrintF("Full Compiler - ");
  }
286
  CodeGenerator::MakeCodePrologue(info);
287 288
  const int kInitialBufferSize = 4 * KB;
  MacroAssembler masm(NULL, kInitialBufferSize);
289 290 291
#ifdef ENABLE_GDB_JIT_INTERFACE
  masm.positions_recorder()->StartGDBJITLineInfoRecording();
#endif
292

293
  FullCodeGenerator cgen(&masm);
294
  cgen.Generate(info);
295 296
  if (cgen.HasStackOverflow()) {
    ASSERT(!Top::has_pending_exception());
297
    return false;
298
  }
299
  unsigned table_offset = cgen.EmitStackCheckTable();
300

301
  Code::Flags flags = Code::ComputeFlags(Code::FUNCTION, NOT_IN_LOOP);
302
  Handle<Code> code = CodeGenerator::MakeCodeEpilogue(&masm, flags, info);
303 304 305 306 307 308
  code->set_optimizable(info->IsOptimizable());
  cgen.PopulateDeoptimizationData(code);
  code->set_has_deoptimization_support(info->HasDeoptimizationSupport());
  code->set_allow_osr_at_loop_nesting_level(0);
  code->set_stack_check_table_start(table_offset);
  CodeGenerator::PrintCode(code, info);
309
  info->SetCode(code);  // may be an empty handle.
310
#ifdef ENABLE_GDB_JIT_INTERFACE
311
  if (FLAG_gdbjit && !code.is_null()) {
312 313 314 315 316 317
    GDBJITLineInfo* lineinfo =
        masm.positions_recorder()->DetachGDBJITLineInfo();

    GDBJIT(RegisterDetailedLineInfo(*code, lineinfo));
  }
#endif
318
  return !code.is_null();
319 320 321
}


322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
unsigned FullCodeGenerator::EmitStackCheckTable() {
  // The stack check table consists of a length (in number of entries)
  // field, and then a sequence of entries.  Each entry is a pair of AST id
  // and code-relative pc offset.
  masm()->Align(kIntSize);
  masm()->RecordComment("[ Stack check table");
  unsigned offset = masm()->pc_offset();
  unsigned length = stack_checks_.length();
  __ dd(length);
  for (unsigned i = 0; i < length; ++i) {
    __ dd(stack_checks_[i].id);
    __ dd(stack_checks_[i].pc_and_state);
  }
  masm()->RecordComment("]");
  return offset;
}


void FullCodeGenerator::PopulateDeoptimizationData(Handle<Code> code) {
  // Fill in the deoptimization information.
  ASSERT(info_->HasDeoptimizationSupport() || bailout_entries_.is_empty());
  if (!info_->HasDeoptimizationSupport()) return;
  int length = bailout_entries_.length();
  Handle<DeoptimizationOutputData> data =
      Factory::NewDeoptimizationOutputData(length, TENURED);
  for (int i = 0; i < length; i++) {
    data->SetAstId(i, Smi::FromInt(bailout_entries_[i].id));
    data->SetPcAndState(i, Smi::FromInt(bailout_entries_[i].pc_and_state));
  }
  code->set_deoptimization_data(*data);
}


void FullCodeGenerator::PrepareForBailout(AstNode* node, State state) {
  PrepareForBailoutForId(node->id(), state);
}


void FullCodeGenerator::RecordJSReturnSite(Call* call) {
  // We record the offset of the function return so we can rebuild the frame
  // if the function was inlined, i.e., this is the return address in the
  // inlined function's frame.
  //
  // The state is ignored.  We defensively set it to TOS_REG, which is the
  // real state of the unoptimized code at the return site.
  PrepareForBailoutForId(call->ReturnId(), TOS_REG);
#ifdef DEBUG
  // In debug builds, mark the return so we can verify that this function
  // was called.
  ASSERT(!call->return_is_recorded_);
  call->return_is_recorded_ = true;
#endif
}


void FullCodeGenerator::PrepareForBailoutForId(int id, State state) {
  // There's no need to prepare this code for bailouts from already optimized
  // code or code that can't be optimized.
  if (!FLAG_deopt || !info_->HasDeoptimizationSupport()) return;
  unsigned pc_and_state =
      StateField::encode(state) | PcField::encode(masm_->pc_offset());
  BailoutEntry entry = { id, pc_and_state };
#ifdef DEBUG
  // Assert that we don't have multiple bailout entries for the same node.
  for (int i = 0; i < bailout_entries_.length(); i++) {
    if (bailout_entries_.at(i).id == entry.id) {
      AstPrinter printer;
      PrintF("%s", printer.PrintProgram(info_->function()));
      UNREACHABLE();
    }
  }
#endif  // DEBUG
  bailout_entries_.Add(entry);
}


void FullCodeGenerator::RecordStackCheck(int ast_id) {
  // The pc offset does not need to be encoded and packed together with a
  // state.
  BailoutEntry entry = { ast_id, masm_->pc_offset() };
  stack_checks_.Add(entry);
}


406
int FullCodeGenerator::SlotOffset(Slot* slot) {
407
  ASSERT(slot != NULL);
408 409 410 411 412
  // Offset is negative because higher indexes are at lower addresses.
  int offset = -slot->index() * kPointerSize;
  // Adjust by a (parameter or local) base offset.
  switch (slot->type()) {
    case Slot::PARAMETER:
413
      offset += (scope()->num_parameters() + 1) * kPointerSize;
414 415 416 417
      break;
    case Slot::LOCAL:
      offset += JavaScriptFrameConstants::kLocal0Offset;
      break;
418 419
    case Slot::CONTEXT:
    case Slot::LOOKUP:
420 421 422 423 424
      UNREACHABLE();
  }
  return offset;
}

425

426
bool FullCodeGenerator::ShouldInlineSmiCase(Token::Value op) {
427 428
  // Inline smi case inside loops, but not division and modulo which
  // are too complicated and take up too much space.
429 430 431
  if (op == Token::DIV ||op == Token::MOD) return false;
  if (FLAG_always_inline_smi_code) return true;
  return loop_depth_ > 0;
432 433 434
}


435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
void FullCodeGenerator::EffectContext::Plug(Register reg) const {
}


void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
  __ Move(result_register(), reg);
}


void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
  __ push(reg);
}


void FullCodeGenerator::TestContext::Plug(Register reg) const {
  // For simplicity we always test the accumulator register.
  __ Move(result_register(), reg);
452
  codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  codegen()->DoTest(true_label_, false_label_, fall_through_);
}


void FullCodeGenerator::EffectContext::PlugTOS() const {
  __ Drop(1);
}


void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
  __ pop(result_register());
}


void FullCodeGenerator::StackValueContext::PlugTOS() const {
}


void FullCodeGenerator::TestContext::PlugTOS() const {
  // For simplicity we always test the accumulator register.
  __ pop(result_register());
474
  codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
  codegen()->DoTest(true_label_, false_label_, fall_through_);
}


void FullCodeGenerator::EffectContext::PrepareTest(
    Label* materialize_true,
    Label* materialize_false,
    Label** if_true,
    Label** if_false,
    Label** fall_through) const {
  // In an effect context, the true and the false case branch to the
  // same label.
  *if_true = *if_false = *fall_through = materialize_true;
}


void FullCodeGenerator::AccumulatorValueContext::PrepareTest(
    Label* materialize_true,
    Label* materialize_false,
    Label** if_true,
    Label** if_false,
    Label** fall_through) const {
  *if_true = *fall_through = materialize_true;
  *if_false = materialize_false;
}


void FullCodeGenerator::StackValueContext::PrepareTest(
    Label* materialize_true,
    Label* materialize_false,
    Label** if_true,
    Label** if_false,
    Label** fall_through) const {
  *if_true = *fall_through = materialize_true;
  *if_false = materialize_false;
}


void FullCodeGenerator::TestContext::PrepareTest(
    Label* materialize_true,
    Label* materialize_false,
    Label** if_true,
    Label** if_false,
    Label** fall_through) const {
  *if_true = true_label_;
  *if_false = false_label_;
  *fall_through = fall_through_;
522 523 524
}


525
void FullCodeGenerator::VisitDeclarations(
526 527 528 529
    ZoneList<Declaration*>* declarations) {
  int length = declarations->length();
  int globals = 0;
  for (int i = 0; i < length; i++) {
530 531
    Declaration* decl = declarations->at(i);
    Variable* var = decl->proxy()->var();
532
    Slot* slot = var->AsSlot();
533 534 535 536 537

    // If it was not possible to allocate the variable at compile
    // time, we need to "declare" it at runtime to make sure it
    // actually exists in the local context.
    if ((slot != NULL && slot->type() == Slot::LOOKUP) || !var->is_global()) {
538
      VisitDeclaration(decl);
539 540 541 542 543 544 545
    } else {
      // Count global variables and functions for later processing
      globals++;
    }
  }

  // Compute array of global variable and function declarations.
546 547 548 549 550 551
  // Do nothing in case of no declared global functions or variables.
  if (globals > 0) {
    Handle<FixedArray> array = Factory::NewFixedArray(2 * globals, TENURED);
    for (int j = 0, i = 0; i < length; i++) {
      Declaration* decl = declarations->at(i);
      Variable* var = decl->proxy()->var();
552
      Slot* slot = var->AsSlot();
553 554 555 556 557 558 559 560 561 562

      if ((slot == NULL || slot->type() != Slot::LOOKUP) && var->is_global()) {
        array->set(j++, *(var->name()));
        if (decl->fun() == NULL) {
          if (var->mode() == Variable::CONST) {
            // In case this is const property use the hole.
            array->set_the_hole(j++);
          } else {
            array->set_undefined(j++);
          }
563
        } else {
564
          Handle<SharedFunctionInfo> function =
565
              Compiler::BuildFunctionInfo(decl->fun(), script());
566
          // Check for stack-overflow exception.
567 568 569 570
          if (function.is_null()) {
            SetStackOverflow();
            return;
          }
571
          array->set(j++, *function);
572 573 574
        }
      }
    }
575 576 577
    // Invoke the platform-dependent code generator to do the actual
    // declaration the global variables and functions.
    DeclareGlobals(array);
578 579 580 581
  }
}


582
void FullCodeGenerator::SetFunctionPosition(FunctionLiteral* fun) {
583 584 585 586 587 588
  if (FLAG_debug_info) {
    CodeGenerator::RecordPositions(masm_, fun->start_position());
  }
}


589
void FullCodeGenerator::SetReturnPosition(FunctionLiteral* fun) {
590
  if (FLAG_debug_info) {
591
    CodeGenerator::RecordPositions(masm_, fun->end_position() - 1);
592 593 594 595
  }
}


596
void FullCodeGenerator::SetStatementPosition(Statement* stmt) {
597
  if (FLAG_debug_info) {
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
#ifdef ENABLE_DEBUGGER_SUPPORT
    if (!Debugger::IsDebuggerActive()) {
      CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
    } else {
      // Check if the statement will be breakable without adding a debug break
      // slot.
      BreakableStatementChecker checker;
      checker.Check(stmt);
      // Record the statement position right here if the statement is not
      // breakable. For breakable statements the actual recording of the
      // position will be postponed to the breakable code (typically an IC).
      bool position_recorded = CodeGenerator::RecordPositions(
          masm_, stmt->statement_pos(), !checker.is_breakable());
      // If the position recording did record a new position generate a debug
      // break slot to make the statement breakable.
      if (position_recorded) {
        Debug::GenerateSlot(masm_);
      }
    }
#else
618
    CodeGenerator::RecordPositions(masm_, stmt->statement_pos());
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
#endif
  }
}


void FullCodeGenerator::SetExpressionPosition(Expression* expr, int pos) {
  if (FLAG_debug_info) {
#ifdef ENABLE_DEBUGGER_SUPPORT
    if (!Debugger::IsDebuggerActive()) {
      CodeGenerator::RecordPositions(masm_, pos);
    } else {
      // Check if the expression will be breakable without adding a debug break
      // slot.
      BreakableStatementChecker checker;
      checker.Check(expr);
      // Record a statement position right here if the expression is not
      // breakable. For breakable expressions the actual recording of the
      // position will be postponed to the breakable code (typically an IC).
      // NOTE this will record a statement position for something which might
      // not be a statement. As stepping in the debugger will only stop at
      // statement positions this is used for e.g. the condition expression of
      // a do while loop.
      bool position_recorded = CodeGenerator::RecordPositions(
          masm_, pos, !checker.is_breakable());
      // If the position recording did record a new position generate a debug
      // break slot to make the statement breakable.
      if (position_recorded) {
        Debug::GenerateSlot(masm_);
      }
    }
#else
    CodeGenerator::RecordPositions(masm_, pos);
#endif
652 653 654 655
  }
}


656
void FullCodeGenerator::SetStatementPosition(int pos) {
657 658 659 660 661 662
  if (FLAG_debug_info) {
    CodeGenerator::RecordPositions(masm_, pos);
  }
}


663
void FullCodeGenerator::SetSourcePosition(int pos) {
664
  if (FLAG_debug_info && pos != RelocInfo::kNoPosition) {
665
    masm_->positions_recorder()->RecordPosition(pos);
666 667 668
  }
}

669

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
// Lookup table for code generators for  special runtime calls which are
// generated inline.
#define INLINE_FUNCTION_GENERATOR_ADDRESS(Name, argc, ressize)          \
    &FullCodeGenerator::Emit##Name,

const FullCodeGenerator::InlineFunctionGenerator
  FullCodeGenerator::kInlineFunctionGenerators[] = {
    INLINE_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
    INLINE_RUNTIME_FUNCTION_LIST(INLINE_FUNCTION_GENERATOR_ADDRESS)
  };
#undef INLINE_FUNCTION_GENERATOR_ADDRESS


FullCodeGenerator::InlineFunctionGenerator
  FullCodeGenerator::FindInlineFunctionGenerator(Runtime::FunctionId id) {
685 686 687 688 689 690
    int lookup_index =
        static_cast<int>(id) - static_cast<int>(Runtime::kFirstInlineFunction);
    ASSERT(lookup_index >= 0);
    ASSERT(static_cast<size_t>(lookup_index) <
           ARRAY_SIZE(kInlineFunctionGenerators));
    return kInlineFunctionGenerators[lookup_index];
691 692 693 694 695 696 697 698 699 700 701 702
}


void FullCodeGenerator::EmitInlineRuntimeCall(CallRuntime* node) {
  ZoneList<Expression*>* args = node->arguments();
  Handle<String> name = node->name();
  Runtime::Function* function = node->function();
  ASSERT(function != NULL);
  ASSERT(function->intrinsic_type == Runtime::INLINE);
  InlineFunctionGenerator generator =
      FindInlineFunctionGenerator(function->function_id);
  ((*this).*(generator))(args);
703 704 705
}


706 707
void FullCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
  Comment cmnt(masm_, "[ BinaryOperation");
708 709 710 711 712 713 714 715 716
  Token::Value op = expr->op();
  Expression* left = expr->left();
  Expression* right = expr->right();

  OverwriteMode mode = NO_OVERWRITE;
  if (left->ResultOverwriteAllowed()) {
    mode = OVERWRITE_LEFT;
  } else if (right->ResultOverwriteAllowed()) {
    mode = OVERWRITE_RIGHT;
717 718
  }

719
  switch (op) {
720
    case Token::COMMA:
721
      VisitForEffect(left);
722 723
      if (context()->IsTest()) ForwardBailoutToChild(expr);
      context()->HandleExpression(right);
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
      break;

    case Token::OR:
    case Token::AND:
      EmitLogicalOperation(expr);
      break;

    case Token::ADD:
    case Token::SUB:
    case Token::DIV:
    case Token::MOD:
    case Token::MUL:
    case Token::BIT_OR:
    case Token::BIT_AND:
    case Token::BIT_XOR:
    case Token::SHL:
    case Token::SHR:
741 742 743 744 745 746 747 748
    case Token::SAR: {
      // Figure out if either of the operands is a constant.
      ConstantOperand constant = ShouldInlineSmiCase(op)
          ? GetConstantOperand(op, left, right)
          : kNoConstants;

      // Load only the operands that we need to materialize.
      if (constant == kNoConstants) {
749 750
        VisitForStackValue(left);
        VisitForAccumulatorValue(right);
751
      } else if (constant == kRightConstant) {
752
        VisitForAccumulatorValue(left);
753 754
      } else {
        ASSERT(constant == kLeftConstant);
755
        VisitForAccumulatorValue(right);
756 757
      }

758
      SetSourcePosition(expr->position());
759
      if (ShouldInlineSmiCase(op)) {
760
        EmitInlineSmiBinaryOp(expr, op, mode, left, right, constant);
761
      } else {
762
        EmitBinaryOp(op, mode);
763
      }
764
      break;
765
    }
766 767 768 769 770 771 772

    default:
      UNREACHABLE();
  }
}


773
void FullCodeGenerator::EmitLogicalOperation(BinaryOperation* expr) {
774 775
  Label eval_right, done;

776
  context()->EmitLogicalLeft(expr, &eval_right, &done);
777

778
  PrepareForBailoutForId(expr->RightId(), NO_REGISTERS);
779
  __ bind(&eval_right);
780 781
  if (context()->IsTest()) ForwardBailoutToChild(expr);
  context()->HandleExpression(expr->right());
782 783 784 785 786

  __ bind(&done);
}


787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
void FullCodeGenerator::EffectContext::EmitLogicalLeft(BinaryOperation* expr,
                                                       Label* eval_right,
                                                       Label* done) const {
  if (expr->op() == Token::OR) {
    codegen()->VisitForControl(expr->left(), done, eval_right, eval_right);
  } else {
    ASSERT(expr->op() == Token::AND);
    codegen()->VisitForControl(expr->left(), eval_right, done, eval_right);
  }
}


void FullCodeGenerator::AccumulatorValueContext::EmitLogicalLeft(
    BinaryOperation* expr,
    Label* eval_right,
    Label* done) const {
803
  HandleExpression(expr->left());
804 805
  // We want the value in the accumulator for the test, and on the stack in case
  // we need it.
806
  __ push(result_register());
807 808
  Label discard, restore;
  if (expr->op() == Token::OR) {
809
    codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
810 811 812
    codegen()->DoTest(&restore, &discard, &restore);
  } else {
    ASSERT(expr->op() == Token::AND);
813
    codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
814 815 816 817 818 819 820 821
    codegen()->DoTest(&discard, &restore, &restore);
  }
  __ bind(&restore);
  __ pop(result_register());
  __ jmp(done);
  __ bind(&discard);
  __ Drop(1);
}
822

823 824 825 826 827 828 829 830 831

void FullCodeGenerator::StackValueContext::EmitLogicalLeft(
    BinaryOperation* expr,
    Label* eval_right,
    Label* done) const {
  codegen()->VisitForAccumulatorValue(expr->left());
  // We want the value in the accumulator for the test, and on the stack in case
  // we need it.
  __ push(result_register());
832
  Label discard;
833
  if (expr->op() == Token::OR) {
834
    codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
835 836 837
    codegen()->DoTest(done, &discard, &discard);
  } else {
    ASSERT(expr->op() == Token::AND);
838
    codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
839
    codegen()->DoTest(&discard, done, &discard);
840 841 842 843 844 845
  }
  __ bind(&discard);
  __ Drop(1);
}


846 847 848 849 850 851 852 853 854 855 856 857 858 859
void FullCodeGenerator::TestContext::EmitLogicalLeft(BinaryOperation* expr,
                                                     Label* eval_right,
                                                     Label* done) const {
  if (expr->op() == Token::OR) {
    codegen()->VisitForControl(expr->left(),
                               true_label_, eval_right, eval_right);
  } else {
    ASSERT(expr->op() == Token::AND);
    codegen()->VisitForControl(expr->left(),
                               eval_right, false_label_, eval_right);
  }
}


860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
void FullCodeGenerator::ForwardBailoutToChild(Expression* expr) {
  if (!info_->HasDeoptimizationSupport()) return;
  ASSERT(context()->IsTest());
  ASSERT(expr == forward_bailout_stack_->expr());
  forward_bailout_pending_ = forward_bailout_stack_;
}


void FullCodeGenerator::EffectContext::HandleExpression(
    Expression* expr) const {
  codegen()->HandleInNonTestContext(expr, NO_REGISTERS);
}


void FullCodeGenerator::AccumulatorValueContext::HandleExpression(
    Expression* expr) const {
  codegen()->HandleInNonTestContext(expr, TOS_REG);
}


void FullCodeGenerator::StackValueContext::HandleExpression(
    Expression* expr) const {
  codegen()->HandleInNonTestContext(expr, NO_REGISTERS);
}


void FullCodeGenerator::TestContext::HandleExpression(Expression* expr) const {
  codegen()->VisitInTestContext(expr);
}


void FullCodeGenerator::HandleInNonTestContext(Expression* expr, State state) {
  ASSERT(forward_bailout_pending_ == NULL);
  AstVisitor::Visit(expr);
  PrepareForBailout(expr, state);
  // Forwarding bailouts to children is a one shot operation. It
  // should have been processed at this point.
  ASSERT(forward_bailout_pending_ == NULL);
}


void FullCodeGenerator::VisitInTestContext(Expression* expr) {
  ForwardBailoutStack stack(expr, forward_bailout_pending_);
  ForwardBailoutStack* saved = forward_bailout_stack_;
  forward_bailout_pending_ = NULL;
  forward_bailout_stack_ = &stack;
  AstVisitor::Visit(expr);
  forward_bailout_stack_ = saved;
}


911
void FullCodeGenerator::VisitBlock(Block* stmt) {
912
  Comment cmnt(masm_, "[ Block");
913
  Breakable nested_statement(this, stmt);
914
  SetStatementPosition(stmt);
915 916

  PrepareForBailoutForId(stmt->EntryId(), TOS_REG);
917
  VisitStatements(stmt->statements());
918
  __ bind(nested_statement.break_target());
919
  PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
920 921 922
}


923
void FullCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
924 925
  Comment cmnt(masm_, "[ ExpressionStatement");
  SetStatementPosition(stmt);
926
  VisitForEffect(stmt->expression());
927 928 929
}


930
void FullCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
931 932
  Comment cmnt(masm_, "[ EmptyStatement");
  SetStatementPosition(stmt);
933 934 935
}


936
void FullCodeGenerator::VisitIfStatement(IfStatement* stmt) {
937
  Comment cmnt(masm_, "[ IfStatement");
938
  SetStatementPosition(stmt);
939 940
  Label then_part, else_part, done;

941 942
  if (stmt->HasElseStatement()) {
    VisitForControl(stmt->condition(), &then_part, &else_part, &then_part);
943
    PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
944 945 946
    __ bind(&then_part);
    Visit(stmt->then_statement());
    __ jmp(&done);
947

948
    PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
949 950 951 952
    __ bind(&else_part);
    Visit(stmt->else_statement());
  } else {
    VisitForControl(stmt->condition(), &then_part, &done, &then_part);
953
    PrepareForBailoutForId(stmt->ThenId(), NO_REGISTERS);
954 955
    __ bind(&then_part);
    Visit(stmt->then_statement());
956 957

    PrepareForBailoutForId(stmt->ElseId(), NO_REGISTERS);
958
  }
959
  __ bind(&done);
960
  PrepareForBailoutForId(stmt->id(), NO_REGISTERS);
961 962 963
}


964
void FullCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
965
  Comment cmnt(masm_,  "[ ContinueStatement");
966
  SetStatementPosition(stmt);
967 968
  NestedStatement* current = nesting_stack_;
  int stack_depth = 0;
kmillikin@chromium.org's avatar
kmillikin@chromium.org committed
969 970 971 972 973
  // When continuing, we clobber the unpredictable value in the accumulator
  // with one that's safe for GC.  If we hit an exit from the try block of
  // try...finally on our way out, we will unconditionally preserve the
  // accumulator on the stack.
  ClearAccumulator();
974 975 976 977 978 979 980 981
  while (!current->IsContinueTarget(stmt->target())) {
    stack_depth = current->Exit(stack_depth);
    current = current->outer();
  }
  __ Drop(stack_depth);

  Iteration* loop = current->AsIteration();
  __ jmp(loop->continue_target());
982 983 984
}


985
void FullCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
986
  Comment cmnt(masm_,  "[ BreakStatement");
987
  SetStatementPosition(stmt);
988 989
  NestedStatement* current = nesting_stack_;
  int stack_depth = 0;
kmillikin@chromium.org's avatar
kmillikin@chromium.org committed
990 991 992 993 994
  // When breaking, we clobber the unpredictable value in the accumulator
  // with one that's safe for GC.  If we hit an exit from the try block of
  // try...finally on our way out, we will unconditionally preserve the
  // accumulator on the stack.
  ClearAccumulator();
995 996 997 998 999 1000 1001 1002
  while (!current->IsBreakTarget(stmt->target())) {
    stack_depth = current->Exit(stack_depth);
    current = current->outer();
  }
  __ Drop(stack_depth);

  Breakable* target = current->AsBreakable();
  __ jmp(target->break_target());
1003 1004 1005
}


1006
void FullCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
1007
  Comment cmnt(masm_, "[ ReturnStatement");
1008
  SetStatementPosition(stmt);
1009
  Expression* expr = stmt->expression();
1010
  VisitForAccumulatorValue(expr);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

  // Exit all nested statements.
  NestedStatement* current = nesting_stack_;
  int stack_depth = 0;
  while (current != NULL) {
    stack_depth = current->Exit(stack_depth);
    current = current->outer();
  }
  __ Drop(stack_depth);

1021
  EmitReturnSequence();
1022 1023 1024
}


1025
void FullCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) {
1026 1027 1028
  Comment cmnt(masm_, "[ WithEnterStatement");
  SetStatementPosition(stmt);

1029
  VisitForStackValue(stmt->expression());
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
  if (stmt->is_catch_block()) {
    __ CallRuntime(Runtime::kPushCatchContext, 1);
  } else {
    __ CallRuntime(Runtime::kPushContext, 1);
  }
  // Both runtime calls return the new context in both the context and the
  // result registers.

  // Update local stack frame context field.
  StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1040 1041 1042
}


1043
void FullCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) {
1044 1045 1046 1047 1048 1049 1050
  Comment cmnt(masm_, "[ WithExitStatement");
  SetStatementPosition(stmt);

  // Pop context.
  LoadContextField(context_register(), Context::PREVIOUS_INDEX);
  // Update local stack frame context field.
  StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
1051 1052 1053
}


1054
void FullCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
1055
  Comment cmnt(masm_, "[ DoWhileStatement");
1056
  SetStatementPosition(stmt);
1057
  Label body, stack_check;
1058 1059

  Iteration loop_statement(this, stmt);
1060 1061 1062 1063 1064
  increment_loop_depth();

  __ bind(&body);
  Visit(stmt->body());

1065 1066
  // Record the position of the do while condition and make sure it is
  // possible to break on the condition.
1067 1068
  __ bind(loop_statement.continue_target());
  PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
1069
  SetExpressionPosition(stmt->cond(), stmt->condition_position());
1070
  VisitForControl(stmt->cond(),
1071
                  &stack_check,
1072
                  loop_statement.break_target(),
1073
                  &stack_check);
1074

1075
  // Check stack before looping.
1076
  PrepareForBailoutForId(stmt->BackEdgeId(), NO_REGISTERS);
1077 1078 1079
  __ bind(&stack_check);
  EmitStackCheck(stmt);
  __ jmp(&body);
1080

1081
  PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1082
  __ bind(loop_statement.break_target());
1083
  decrement_loop_depth();
1084 1085 1086
}


1087
void FullCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
1088
  Comment cmnt(masm_, "[ WhileStatement");
1089
  Label test, body;
1090 1091

  Iteration loop_statement(this, stmt);
1092 1093 1094
  increment_loop_depth();

  // Emit the test at the bottom of the loop.
1095
  __ jmp(&test);
1096

1097
  PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1098 1099
  __ bind(&body);
  Visit(stmt->body());
1100 1101 1102

  // Emit the statement position here as this is where the while
  // statement code starts.
1103
  __ bind(loop_statement.continue_target());
1104
  SetStatementPosition(stmt);
1105

1106
  // Check stack before looping.
1107
  EmitStackCheck(stmt);
1108

1109
  __ bind(&test);
1110 1111 1112 1113
  VisitForControl(stmt->cond(),
                  &body,
                  loop_statement.break_target(),
                  loop_statement.break_target());
1114

1115
  PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1116
  __ bind(loop_statement.break_target());
1117
  decrement_loop_depth();
1118 1119 1120
}


1121
void FullCodeGenerator::VisitForStatement(ForStatement* stmt) {
1122
  Comment cmnt(masm_, "[ ForStatement");
1123
  Label test, body;
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133

  Iteration loop_statement(this, stmt);
  if (stmt->init() != NULL) {
    Visit(stmt->init());
  }

  increment_loop_depth();
  // Emit the test at the bottom of the loop (even if empty).
  __ jmp(&test);

1134
  PrepareForBailoutForId(stmt->BodyId(), NO_REGISTERS);
1135 1136 1137
  __ bind(&body);
  Visit(stmt->body());

1138
  PrepareForBailoutForId(stmt->ContinueId(), NO_REGISTERS);
1139
  __ bind(loop_statement.continue_target());
1140 1141 1142 1143 1144
  SetStatementPosition(stmt);
  if (stmt->next() != NULL) {
    Visit(stmt->next());
  }

1145 1146
  // Emit the statement position here as this is where the for
  // statement code starts.
1147
  SetStatementPosition(stmt);
1148 1149

  // Check stack before looping.
1150
  EmitStackCheck(stmt);
1151

1152
  __ bind(&test);
1153
  if (stmt->cond() != NULL) {
1154 1155 1156 1157
    VisitForControl(stmt->cond(),
                    &body,
                    loop_statement.break_target(),
                    loop_statement.break_target());
1158 1159 1160 1161
  } else {
    __ jmp(&body);
  }

1162
  PrepareForBailoutForId(stmt->ExitId(), NO_REGISTERS);
1163
  __ bind(loop_statement.break_target());
1164
  decrement_loop_depth();
1165 1166 1167
}


1168
void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
  Comment cmnt(masm_, "[ TryCatchStatement");
  SetStatementPosition(stmt);
  // The try block adds a handler to the exception handler chain
  // before entering, and removes it again when exiting normally.
  // If an exception is thrown during execution of the try block,
  // control is passed to the handler, which also consumes the handler.
  // At this point, the exception is in a register, and store it in
  // the temporary local variable (prints as ".catch-var") before
  // executing the catch block. The catch block has been rewritten
  // to introduce a new scope to bind the catch variable and to remove
  // that scope again afterwards.

  Label try_handler_setup, catch_entry, done;
  __ Call(&try_handler_setup);
  // Try handler code, exception in result register.

  // Store exception in local .catch variable before executing catch block.
  {
    // The catch variable is *always* a variable proxy for a local variable.
    Variable* catch_var = stmt->catch_var()->AsVariableProxy()->AsVariable();
    ASSERT_NOT_NULL(catch_var);
1190
    Slot* variable_slot = catch_var->AsSlot();
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    ASSERT_NOT_NULL(variable_slot);
    ASSERT_EQ(Slot::LOCAL, variable_slot->type());
    StoreToFrameField(SlotOffset(variable_slot), result_register());
  }

  Visit(stmt->catch_block());
  __ jmp(&done);

  // Try block code. Sets up the exception handler chain.
  __ bind(&try_handler_setup);
  {
    TryCatch try_block(this, &catch_entry);
    __ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
    Visit(stmt->try_block());
    __ PopTryHandler();
  }
  __ bind(&done);
1208 1209 1210
}


1211
void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
1212 1213
  Comment cmnt(masm_, "[ TryFinallyStatement");
  SetStatementPosition(stmt);
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
  // Try finally is compiled by setting up a try-handler on the stack while
  // executing the try body, and removing it again afterwards.
  //
  // The try-finally construct can enter the finally block in three ways:
  // 1. By exiting the try-block normally. This removes the try-handler and
  //      calls the finally block code before continuing.
  // 2. By exiting the try-block with a function-local control flow transfer
  //    (break/continue/return). The site of the, e.g., break removes the
  //    try handler and calls the finally block code before continuing
  //    its outward control transfer.
  // 3. by exiting the try-block with a thrown exception.
  //    This can happen in nested function calls. It traverses the try-handler
1226
  //    chain and consumes the try-handler entry before jumping to the
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
  //    handler code. The handler code then calls the finally-block before
  //    rethrowing the exception.
  //
  // The finally block must assume a return address on top of the stack
  // (or in the link register on ARM chips) and a value (return value or
  // exception) in the result register (rax/eax/r0), both of which must
  // be preserved. The return address isn't GC-safe, so it should be
  // cooked before GC.
  Label finally_entry;
  Label try_handler_setup;

  // Setup the try-handler chain. Use a call to
  // Jump to try-handler setup and try-block code. Use call to put try-handler
  // address on stack.
  __ Call(&try_handler_setup);
  // Try handler code. Return address of call is pushed on handler stack.
  {
    // This code is only executed during stack-handler traversal when an
    // exception is thrown. The execption is in the result register, which
    // is retained by the finally block.
    // Call the finally block and then rethrow the exception.
    __ Call(&finally_entry);
1249 1250
    __ push(result_register());
    __ CallRuntime(Runtime::kReThrow, 1);
1251 1252 1253 1254 1255 1256
  }

  __ bind(&finally_entry);
  {
    // Finally block implementation.
    Finally finally_block(this);
1257
    EnterFinallyBlock();
1258 1259 1260 1261 1262 1263 1264 1265
    Visit(stmt->finally_block());
    ExitFinallyBlock();  // Return to the calling code.
  }

  __ bind(&try_handler_setup);
  {
    // Setup try handler (stack pointer registers).
    TryFinally try_block(this, &finally_entry);
1266 1267
    __ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
    Visit(stmt->try_block());
1268 1269
    __ PopTryHandler();
  }
kmillikin@chromium.org's avatar
kmillikin@chromium.org committed
1270 1271 1272 1273
  // Execute the finally block on the way out.  Clobber the unpredictable
  // value in the accumulator with one that's safe for GC.  The finally
  // block will unconditionally preserve the accumulator on the stack.
  ClearAccumulator();
1274
  __ Call(&finally_entry);
1275 1276 1277
}


1278
void FullCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
1279 1280 1281
#ifdef ENABLE_DEBUGGER_SUPPORT
  Comment cmnt(masm_, "[ DebuggerStatement");
  SetStatementPosition(stmt);
1282

serya@chromium.org's avatar
serya@chromium.org committed
1283
  __ DebugBreak();
1284 1285
  // Ignore the return value.
#endif
1286 1287 1288
}


1289
void FullCodeGenerator::VisitConditional(Conditional* expr) {
1290
  Comment cmnt(masm_, "[ Conditional");
1291
  Label true_case, false_case, done;
1292
  VisitForControl(expr->condition(), &true_case, &false_case, &true_case);
1293

1294
  PrepareForBailoutForId(expr->ThenId(), NO_REGISTERS);
1295
  __ bind(&true_case);
1296 1297
  SetExpressionPosition(expr->then_expression(),
                        expr->then_expression_position());
1298 1299 1300 1301 1302 1303 1304
  if (context()->IsTest()) {
    const TestContext* for_test = TestContext::cast(context());
    VisitForControl(expr->then_expression(),
                    for_test->true_label(),
                    for_test->false_label(),
                    NULL);
  } else {
1305
    context()->HandleExpression(expr->then_expression());
1306 1307 1308
    __ jmp(&done);
  }

1309
  PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
1310
  __ bind(&false_case);
1311
  if (context()->IsTest()) ForwardBailoutToChild(expr);
1312 1313
  SetExpressionPosition(expr->else_expression(),
                        expr->else_expression_position());
1314
  context()->HandleExpression(expr->else_expression());
1315
  // If control flow falls through Visit, merge it with true case here.
1316
  if (!context()->IsTest()) {
1317 1318
    __ bind(&done);
  }
1319 1320 1321
}


1322
void FullCodeGenerator::VisitLiteral(Literal* expr) {
1323
  Comment cmnt(masm_, "[ Literal");
1324
  context()->Plug(expr->handle());
1325 1326 1327
}


1328 1329 1330 1331 1332
void FullCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
  Comment cmnt(masm_, "[ FunctionLiteral");

  // Build the function boilerplate and instantiate it.
  Handle<SharedFunctionInfo> function_info =
1333 1334 1335 1336 1337
      Compiler::BuildFunctionInfo(expr, script());
  if (function_info.is_null()) {
    SetStackOverflow();
    return;
  }
1338
  EmitNewClosure(function_info, expr->pretenure());
1339 1340 1341 1342 1343 1344
}


void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
    SharedFunctionInfoLiteral* expr) {
  Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
1345
  EmitNewClosure(expr->shared_function_info(), false);
1346 1347 1348
}


1349
void FullCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) {
1350 1351 1352
  // Call runtime routine to allocate the catch extension object and
  // assign the exception value to the catch variable.
  Comment cmnt(masm_, "[ CatchExtensionObject");
1353 1354
  VisitForStackValue(expr->key());
  VisitForStackValue(expr->value());
1355 1356
  // Create catch extension object.
  __ CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
1357
  context()->Plug(result_register());
1358 1359 1360
}


1361
void FullCodeGenerator::VisitThrow(Throw* expr) {
1362
  Comment cmnt(masm_, "[ Throw");
1363
  VisitForStackValue(expr->exception());
1364 1365
  __ CallRuntime(Runtime::kThrow, 1);
  // Never returns here.
1366 1367 1368
}


1369 1370 1371 1372 1373
void FullCodeGenerator::VisitIncrementOperation(IncrementOperation* expr) {
  UNREACHABLE();
}


1374
int FullCodeGenerator::TryFinally::Exit(int stack_depth) {
1375 1376 1377 1378 1379 1380 1381 1382
  // The macros used here must preserve the result register.
  __ Drop(stack_depth);
  __ PopTryHandler();
  __ Call(finally_entry_);
  return 0;
}


1383
int FullCodeGenerator::TryCatch::Exit(int stack_depth) {
1384 1385 1386 1387 1388 1389
  // The macros used here must preserve the result register.
  __ Drop(stack_depth);
  __ PopTryHandler();
  return 0;
}

1390

1391 1392 1393
#undef __


1394
} }  // namespace v8::internal