blob: 8cec86926ace3013f0c35a04d8e73ecc4f205b60 [file] [log] [blame]
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +00001// Copyright 2008 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#ifndef V8_JUMP_TARGET_HEAVY_H_
29#define V8_JUMP_TARGET_HEAVY_H_
30
31#include "macro-assembler.h"
32#include "zone-inl.h"
33
34namespace v8 {
35namespace internal {
36
37// Forward declarations.
38class FrameElement;
39class Result;
40class VirtualFrame;
41
42// -------------------------------------------------------------------------
43// Jump targets
44//
45// A jump target is an abstraction of a basic-block entry in generated
46// code. It collects all the virtual frames reaching the block by
47// forward jumps and pairs them with labels for the merge code along
48// all forward-reaching paths. When bound, an expected frame for the
49// block is determined and code is generated to merge to the expected
50// frame. For backward jumps, the merge code is generated at the edge
51// leaving the predecessor block.
52//
53// A jump target must have been reached via control flow (either by
54// jumping, branching, or falling through) at the time it is bound.
55// In particular, this means that at least one of the control-flow
56// graph edges reaching the target must be a forward edge.
57
58class JumpTarget : public ZoneObject { // Shadows are dynamically allocated.
59 public:
60 // Forward-only jump targets can only be reached by forward CFG edges.
61 enum Directionality { FORWARD_ONLY, BIDIRECTIONAL };
62
63 // Construct a jump target used to generate code and to provide
64 // access to a current frame.
65 explicit JumpTarget(Directionality direction)
66 : direction_(direction),
67 reaching_frames_(0),
68 merge_labels_(0),
69 entry_frame_(NULL) {
70 }
71
72 // Construct a jump target.
73 JumpTarget()
74 : direction_(FORWARD_ONLY),
75 reaching_frames_(0),
76 merge_labels_(0),
77 entry_frame_(NULL) {
78 }
79
80 virtual ~JumpTarget() {}
81
82 // Set the direction of the jump target.
83 virtual void set_direction(Directionality direction) {
84 direction_ = direction;
85 }
86
87 // Treat the jump target as a fresh one. The state is reset.
88 void Unuse();
89
90 inline CodeGenerator* cgen();
91
92 Label* entry_label() { return &entry_label_; }
93
94 VirtualFrame* entry_frame() const { return entry_frame_; }
95 void set_entry_frame(VirtualFrame* frame) {
96 entry_frame_ = frame;
97 }
98
99 // Predicates testing the state of the encapsulated label.
100 bool is_bound() const { return entry_label_.is_bound(); }
101 bool is_linked() const {
102 return !is_bound() && !reaching_frames_.is_empty();
103 }
104 bool is_unused() const {
105 // This is !is_bound() && !is_linked().
106 return !is_bound() && reaching_frames_.is_empty();
107 }
108
109 // Emit a jump to the target. There must be a current frame at the
110 // jump and there will be no current frame after the jump.
111 virtual void Jump();
112 virtual void Jump(Result* arg);
113
114 // Emit a conditional branch to the target. There must be a current
115 // frame at the branch. The current frame will fall through to the
116 // code after the branch. The arg is a result that is live both at
117 // the target and the fall-through.
118 virtual void Branch(Condition cc, Hint hint = no_hint);
119 virtual void Branch(Condition cc, Result* arg, Hint hint = no_hint);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000120 void Branch(Condition cc,
121 Result* arg0,
122 Result* arg1,
123 Hint hint = no_hint);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000124
125 // Bind a jump target. If there is no current frame at the binding
126 // site, there must be at least one frame reaching via a forward
127 // jump.
128 virtual void Bind();
129 virtual void Bind(Result* arg);
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000130 void Bind(Result* arg0, Result* arg1);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000131
132 // Emit a call to a jump target. There must be a current frame at
133 // the call. The frame at the target is the same as the current
134 // frame except for an extra return address on top of it. The frame
135 // after the call is the same as the frame before the call.
136 void Call();
137
138 static void set_compiling_deferred_code(bool flag) {
139 compiling_deferred_code_ = flag;
140 }
141
142 protected:
143 // Directionality flag set at initialization time.
144 Directionality direction_;
145
146 // A list of frames reaching this block via forward jumps.
147 ZoneList<VirtualFrame*> reaching_frames_;
148
149 // A parallel list of labels for merge code.
150 ZoneList<Label> merge_labels_;
151
152 // The frame used on entry to the block and expected at backward
153 // jumps to the block. Set when the jump target is bound, but may
154 // or may not be set for forward-only blocks.
155 VirtualFrame* entry_frame_;
156
157 // The actual entry label of the block.
158 Label entry_label_;
159
160 // Implementations of Jump, Branch, and Bind with all arguments and
161 // return values using the virtual frame.
162 void DoJump();
163 void DoBranch(Condition cc, Hint hint);
164 void DoBind();
165
166 private:
167 static bool compiling_deferred_code_;
168
169 // Add a virtual frame reaching this labeled block via a forward jump,
170 // and a corresponding merge code label.
171 void AddReachingFrame(VirtualFrame* frame);
172
173 // Perform initialization required during entry frame computation
174 // after setting the virtual frame element at index in frame to be
175 // target.
176 inline void InitializeEntryElement(int index, FrameElement* target);
177
178 // Compute a frame to use for entry to this block.
179 void ComputeEntryFrame();
180
181 DISALLOW_COPY_AND_ASSIGN(JumpTarget);
182};
183
184
185// -------------------------------------------------------------------------
186// Break targets
187//
188// A break target is a jump target that can be used to break out of a
189// statement that keeps extra state on the stack (eg, for/in or
190// try/finally). They know the expected stack height at the target
191// and will drop state from nested statements as part of merging.
192//
193// Break targets are used for return, break, and continue targets.
194
195class BreakTarget : public JumpTarget {
196 public:
197 // Construct a break target.
198 BreakTarget() {}
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000199 explicit BreakTarget(JumpTarget::Directionality direction)
200 : JumpTarget(direction) { }
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000201
202 virtual ~BreakTarget() {}
203
204 // Set the direction of the break target.
205 virtual void set_direction(Directionality direction);
206
207 // Copy the state of this break target to the destination. The
208 // lists of forward-reaching frames and merge-point labels are
209 // copied. All virtual frame pointers are copied, not the
210 // pointed-to frames. The previous state of the destination is
211 // overwritten, without deallocating pointed-to virtual frames.
212 void CopyTo(BreakTarget* destination);
213
214 // Emit a jump to the target. There must be a current frame at the
215 // jump and there will be no current frame after the jump.
216 virtual void Jump();
217 virtual void Jump(Result* arg);
218
219 // Emit a conditional branch to the target. There must be a current
220 // frame at the branch. The current frame will fall through to the
221 // code after the branch.
222 virtual void Branch(Condition cc, Hint hint = no_hint);
223 virtual void Branch(Condition cc, Result* arg, Hint hint = no_hint);
224
225 // Bind a break target. If there is no current frame at the binding
226 // site, there must be at least one frame reaching via a forward
227 // jump.
228 virtual void Bind();
229 virtual void Bind(Result* arg);
230
231 // Setter for expected height.
232 void set_expected_height(int expected) { expected_height_ = expected; }
233
234 private:
235 // The expected height of the expression stack where the target will
236 // be bound, statically known at initialization time.
237 int expected_height_;
238
239 DISALLOW_COPY_AND_ASSIGN(BreakTarget);
240};
241
242} } // namespace v8::internal
243
244#endif // V8_JUMP_TARGET_HEAVY_H_