blob: 0d653063bd504caa2e2bc0ea55b4e3ea1699b4b6 [file] [log] [blame]
Kristian Monsen25f61362010-05-21 11:50:48 +01001// 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_LIGHT_H_
29#define V8_JUMP_TARGET_LIGHT_H_
30
31#include "macro-assembler.h"
32#include "zone-inl.h"
33#include "virtual-frame.h"
34
35namespace v8 {
36namespace internal {
37
38// Forward declarations.
39class FrameElement;
40class Result;
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.
64 explicit inline JumpTarget(Directionality direction);
65
66 inline JumpTarget();
67
68 virtual ~JumpTarget() {}
69
70 void Unuse() {
71 entry_frame_set_ = false;
72 entry_label_.Unuse();
73 }
74
75 inline CodeGenerator* cgen();
76
Leon Clarkef7060e22010-06-03 12:02:55 +010077 Label* entry_label() { return &entry_label_; }
78
Kristian Monsen25f61362010-05-21 11:50:48 +010079 const VirtualFrame* entry_frame() const {
80 return entry_frame_set_ ? &entry_frame_ : NULL;
81 }
82
83 void set_entry_frame(VirtualFrame* frame) {
84 entry_frame_ = *frame;
85 entry_frame_set_ = true;
86 }
87
88 // Predicates testing the state of the encapsulated label.
89 bool is_bound() const { return entry_label_.is_bound(); }
90 bool is_linked() const { return entry_label_.is_linked(); }
91 bool is_unused() const { return entry_label_.is_unused(); }
92
93 // Copy the state of this jump target to the destination.
94 inline void CopyTo(JumpTarget* destination) {
95 *destination = *this;
96 }
97
98 // Emit a jump to the target. There must be a current frame at the
99 // jump and there will be no current frame after the jump.
100 virtual void Jump();
101
102 // Emit a conditional branch to the target. There must be a current
103 // frame at the branch. The current frame will fall through to the
Kristian Monsen50ef84f2010-07-29 15:18:00 +0100104 // code after the branch.
Kristian Monsen25f61362010-05-21 11:50:48 +0100105 virtual void Branch(Condition cc, Hint hint = no_hint);
106
107 // Bind a jump target. If there is no current frame at the binding
108 // site, there must be at least one frame reaching via a forward
109 // jump.
110 virtual void Bind();
111
112 // Emit a call to a jump target. There must be a current frame at
113 // the call. The frame at the target is the same as the current
114 // frame except for an extra return address on top of it. The frame
115 // after the call is the same as the frame before the call.
116 void Call();
117
118 protected:
119 // Has an entry frame been found?
120 bool entry_frame_set_;
121
Steve Block8defd9f2010-07-08 12:39:36 +0100122 // Can we branch backwards to this label?
123 Directionality direction_;
124
Kristian Monsen25f61362010-05-21 11:50:48 +0100125 // The frame used on entry to the block and expected at backward
126 // jumps to the block. Set the first time something branches to this
127 // jump target.
128 VirtualFrame entry_frame_;
129
130 // The actual entry label of the block.
131 Label entry_label_;
132
133 // Implementations of Jump, Branch, and Bind with all arguments and
134 // return values using the virtual frame.
135 void DoJump();
136 void DoBranch(Condition cc, Hint hint);
137 void DoBind();
138};
139
140
141// -------------------------------------------------------------------------
142// Break targets
143//
144// A break target is a jump target that can be used to break out of a
145// statement that keeps extra state on the stack (eg, for/in or
146// try/finally). They know the expected stack height at the target
147// and will drop state from nested statements as part of merging.
148//
149// Break targets are used for return, break, and continue targets.
150
151class BreakTarget : public JumpTarget {
152 public:
153 // Construct a break target.
154 inline BreakTarget();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100155
Steve Block8defd9f2010-07-08 12:39:36 +0100156 inline BreakTarget(JumpTarget::Directionality direction);
Kristian Monsen25f61362010-05-21 11:50:48 +0100157
158 virtual ~BreakTarget() {}
159
160 // Copy the state of this jump target to the destination.
161 inline void CopyTo(BreakTarget* destination) {
162 *destination = *this;
163 }
164
165 // Emit a jump to the target. There must be a current frame at the
166 // jump and there will be no current frame after the jump.
167 virtual void Jump();
168
169 // Emit a conditional branch to the target. There must be a current
170 // frame at the branch. The current frame will fall through to the
171 // code after the branch.
172 virtual void Branch(Condition cc, Hint hint = no_hint);
173
174 // Bind a break target. If there is no current frame at the binding
175 // site, there must be at least one frame reaching via a forward
176 // jump.
177 virtual void Bind();
178
179 // Setter for expected height.
180 void set_expected_height(int expected) { expected_height_ = expected; }
181
182 // Uses the current frame to set the expected height.
183 void SetExpectedHeight();
184
185 private:
186 // The expected height of the expression stack where the target will
187 // be bound, statically known at initialization time.
188 int expected_height_;
189};
190
191} } // namespace v8::internal
192
193#endif // V8_JUMP_TARGET_LIGHT_H_