summaryrefslogtreecommitdiff
path: root/deps/v8/src/interpreter/control-flow-builders.cc
blob: 6510aa443aaa2a605989865596cc8227406d3a48 (plain)
1
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
31
32
33
34
35
36
37
38
39
40
41
42
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
// 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 {


BreakableControlFlowBuilder::~BreakableControlFlowBuilder() {
  DCHECK(break_sites_.empty());
}


void BreakableControlFlowBuilder::SetBreakTarget(const BytecodeLabel& target) {
  BindLabels(target, &break_sites_);
}


void BreakableControlFlowBuilder::EmitJump(ZoneVector<BytecodeLabel>* sites) {
  sites->push_back(BytecodeLabel());
  builder()->Jump(&sites->back());
}


void BreakableControlFlowBuilder::EmitJumpIfTrue(
    ZoneVector<BytecodeLabel>* sites) {
  sites->push_back(BytecodeLabel());
  builder()->JumpIfTrue(&sites->back());
}


void BreakableControlFlowBuilder::EmitJumpIfFalse(
    ZoneVector<BytecodeLabel>* sites) {
  sites->push_back(BytecodeLabel());
  builder()->JumpIfFalse(&sites->back());
}


void BreakableControlFlowBuilder::EmitJumpIfUndefined(
    ZoneVector<BytecodeLabel>* sites) {
  sites->push_back(BytecodeLabel());
  builder()->JumpIfUndefined(&sites->back());
}


void BreakableControlFlowBuilder::EmitJumpIfNull(
    ZoneVector<BytecodeLabel>* sites) {
  sites->push_back(BytecodeLabel());
  builder()->JumpIfNull(&sites->back());
}


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));
}


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


void BreakableControlFlowBuilder::BindLabels(const BytecodeLabel& target,
                                             ZoneVector<BytecodeLabel>* sites) {
  for (size_t i = 0; i < sites->size(); i++) {
    BytecodeLabel& site = sites->at(i);
    builder()->Bind(target, &site);
  }
  sites->clear();
}


void BlockBuilder::EndBlock() {
  builder()->Bind(&block_end_);
  SetBreakTarget(block_end_);
}


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


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_);
}


void LoopBuilder::EndLoop() {
  // 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_);
  }
}


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);
}


void TryCatchBuilder::BeginTry(Register context) {
  builder()->MarkTryBegin(handler_id_, context);
}


void TryCatchBuilder::EndTry() {
  builder()->MarkTryEnd(handler_id_);
  builder()->Jump(&exit_);
  builder()->Bind(&handler_);
  builder()->MarkHandler(handler_id_, true);
}


void TryCatchBuilder::EndCatch() { builder()->Bind(&exit_); }


void TryFinallyBuilder::BeginTry(Register context) {
  builder()->MarkTryBegin(handler_id_, context);
}


void TryFinallyBuilder::LeaveTry() {
  finalization_sites_.push_back(BytecodeLabel());
  builder()->Jump(&finalization_sites_.back());
}


void TryFinallyBuilder::EndTry() {
  builder()->MarkTryEnd(handler_id_);
}


void TryFinallyBuilder::BeginHandler() {
  builder()->Bind(&handler_);
  builder()->MarkHandler(handler_id_, will_catch_);
}


void TryFinallyBuilder::BeginFinally() {
  for (size_t i = 0; i < finalization_sites_.size(); i++) {
    BytecodeLabel& site = finalization_sites_.at(i);
    builder()->Bind(&site);
  }
}


void TryFinallyBuilder::EndFinally() {
  // Nothing to be done here.
}

}  // namespace interpreter
}  // namespace internal
}  // namespace v8