control-flow-builders.cc 3.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/interpreter/control-flow-builders.h"

namespace v8 {
namespace internal {
namespace interpreter {


12
BreakableControlFlowBuilder::~BreakableControlFlowBuilder() {
13 14 15 16
  DCHECK(break_sites_.empty());
}


17
void BreakableControlFlowBuilder::SetBreakTarget(const BytecodeLabel& target) {
18 19 20 21
  BindLabels(target, &break_sites_);
}


22
void BreakableControlFlowBuilder::EmitJump(ZoneVector<BytecodeLabel>* sites) {
23 24 25 26 27
  sites->push_back(BytecodeLabel());
  builder()->Jump(&sites->back());
}


28 29
void BreakableControlFlowBuilder::EmitJumpIfTrue(
    ZoneVector<BytecodeLabel>* sites) {
30 31 32 33 34
  sites->push_back(BytecodeLabel());
  builder()->JumpIfTrue(&sites->back());
}


35 36 37 38 39 40 41
void BreakableControlFlowBuilder::EmitJumpIfFalse(
    ZoneVector<BytecodeLabel>* sites) {
  sites->push_back(BytecodeLabel());
  builder()->JumpIfFalse(&sites->back());
}


42 43
void BreakableControlFlowBuilder::EmitJumpIfUndefined(
    ZoneVector<BytecodeLabel>* sites) {
44 45 46 47 48
  sites->push_back(BytecodeLabel());
  builder()->JumpIfUndefined(&sites->back());
}


49 50
void BreakableControlFlowBuilder::EmitJumpIfNull(
    ZoneVector<BytecodeLabel>* sites) {
51 52 53 54 55
  sites->push_back(BytecodeLabel());
  builder()->JumpIfNull(&sites->back());
}


56 57 58 59 60 61 62 63 64 65 66 67
void BreakableControlFlowBuilder::EmitJump(ZoneVector<BytecodeLabel>* sites,
                                           int index) {
  builder()->Jump(&sites->at(index));
}


void BreakableControlFlowBuilder::EmitJumpIfTrue(
    ZoneVector<BytecodeLabel>* sites, int index) {
  builder()->JumpIfTrue(&sites->at(index));
}


68 69 70 71 72 73
void BreakableControlFlowBuilder::EmitJumpIfFalse(
    ZoneVector<BytecodeLabel>* sites, int index) {
  builder()->JumpIfFalse(&sites->at(index));
}


74 75
void BreakableControlFlowBuilder::BindLabels(const BytecodeLabel& target,
                                             ZoneVector<BytecodeLabel>* sites) {
76 77 78 79 80 81 82
  for (size_t i = 0; i < sites->size(); i++) {
    BytecodeLabel& site = sites->at(i);
    builder()->Bind(target, &site);
  }
  sites->clear();
}

83

84 85 86 87 88 89
void BlockBuilder::EndBlock() {
  builder()->Bind(&block_end_);
  SetBreakTarget(block_end_);
}


90 91 92
LoopBuilder::~LoopBuilder() { DCHECK(continue_sites_.empty()); }


93 94 95 96 97 98 99 100 101 102
void LoopBuilder::LoopHeader() {
  // Jumps from before the loop header into the loop violate ordering
  // requirements of bytecode basic blocks. The only entry into a loop
  // must be the loop header. Surely breaks is okay? Not if nested
  // and misplaced between the headers.
  DCHECK(break_sites_.empty() && continue_sites_.empty());
  builder()->Bind(&loop_header_);
}


103
void LoopBuilder::EndLoop() {
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  // Loop must have closed form, i.e. all loop elements are within the loop,
  // the loop header precedes the body and next elements in the loop.
  DCHECK(loop_header_.is_bound());
  builder()->Bind(&loop_end_);
  SetBreakTarget(loop_end_);
  if (next_.is_bound()) {
    DCHECK(!condition_.is_bound() || next_.offset() >= condition_.offset());
    SetContinueTarget(next_);
  } else {
    DCHECK(condition_.is_bound());
    DCHECK_GE(condition_.offset(), loop_header_.offset());
    DCHECK_LE(condition_.offset(), loop_end_.offset());
    SetContinueTarget(condition_);
  }
}


121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
void LoopBuilder::SetContinueTarget(const BytecodeLabel& target) {
  BindLabels(target, &continue_sites_);
}


SwitchBuilder::~SwitchBuilder() {
#ifdef DEBUG
  for (auto site : case_sites_) {
    DCHECK(site.is_bound());
  }
#endif
}


void SwitchBuilder::SetCaseTarget(int index) {
  BytecodeLabel& site = case_sites_.at(index);
  builder()->Bind(&site);
}

140 141 142
}  // namespace interpreter
}  // namespace internal
}  // namespace v8