]> git.saurik.com Git - apple/javascriptcore.git/blob - dfg/DFGCorrectableJumpPoint.h
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / dfg / DFGCorrectableJumpPoint.h
1 /*
2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #ifndef DFGCorrectableJumpPoint_h
27 #define DFGCorrectableJumpPoint_h
28
29 #if ENABLE(DFG_JIT)
30
31 #include "LinkBuffer.h"
32 #include "MacroAssembler.h"
33
34 namespace JSC { namespace DFG {
35
36 // This is a type-safe union of MacroAssembler::Jump and CodeLocationJump.
37 // Furthermore, it supports the notion of branching (possibly conditionally, but
38 // also possibly jumping unconditionally) to an out-of-line patchable jump.
39 // Thus it goes through three states:
40 //
41 // 1) Label of unpatchable branch or jump (i.e. MacroAssembler::Jump).
42 // 2) Label of patchable jump (i.e. MacroAssembler::PatchableJump).
43 // 3) Corrected post-linking label of patchable jump (i.e. CodeLocationJump).
44 //
45 // The setting of state (1) corresponds to planting the in-line unpatchable
46 // branch or jump. The state transition (1)->(2) corresponds to linking the
47 // in-line branch or jump to the out-of-line patchable jump, and recording
48 // the latter's label. The state transition (2)->(3) corresponds to recording
49 // the out-of-line patchable jump's location after branch compaction has
50 // completed.
51 //
52 // You can also go directly from the first state to the third state, if you
53 // wish to use this class for in-line patchable jumps.
54
55 class CorrectableJumpPoint {
56 public:
57 CorrectableJumpPoint(MacroAssembler::Jump check)
58 : m_codeOffset(check.m_label.m_offset)
59 #ifndef NDEBUG
60 , m_mode(InitialJump)
61 #endif
62 {
63 #if CPU(ARM_THUMB2)
64 m_type = check.m_type;
65 m_condition = check.m_condition;
66 #endif
67 }
68
69 void switchToLateJump(MacroAssembler::PatchableJump check)
70 {
71 #ifndef NDEBUG
72 ASSERT(m_mode == InitialJump);
73 m_mode = LateJump;
74 #endif
75 // Late jumps should only ever be real jumps.
76 #if CPU(ARM_THUMB2)
77 ASSERT(check.m_jump.m_type == ARMv7Assembler::JumpNoConditionFixedSize);
78 ASSERT(check.m_jump.m_condition == ARMv7Assembler::ConditionInvalid);
79 m_type = ARMv7Assembler::JumpNoConditionFixedSize;
80 m_condition = ARMv7Assembler::ConditionInvalid;
81 #endif
82 m_codeOffset = check.m_jump.m_label.m_offset;
83 }
84
85 void correctInitialJump(LinkBuffer& linkBuffer)
86 {
87 ASSERT(m_mode == InitialJump);
88 #if CPU(ARM_THUMB2)
89 ASSERT(m_type == ARMv7Assembler::JumpNoConditionFixedSize);
90 ASSERT(m_condition == ARMv7Assembler::ConditionInvalid);
91 #endif
92 correctJump(linkBuffer);
93 }
94
95 void correctLateJump(LinkBuffer& linkBuffer)
96 {
97 ASSERT(m_mode == LateJump);
98 correctJump(linkBuffer);
99 }
100
101 MacroAssembler::Jump initialJump() const
102 {
103 ASSERT(m_mode == InitialJump);
104 return getJump();
105 }
106
107 MacroAssembler::Jump lateJump() const
108 {
109 ASSERT(m_mode == LateJump);
110 return getJump();
111 }
112
113 CodeLocationJump codeLocationForRepatch(CodeBlock*) const;
114
115 private:
116 void correctJump(LinkBuffer& linkBuffer)
117 {
118 #ifndef NDEBUG
119 m_mode = CorrectedJump;
120 #endif
121 MacroAssembler::Label label;
122 label.m_label.m_offset = m_codeOffset;
123 m_codeOffset = linkBuffer.offsetOf(label);
124 }
125
126 MacroAssembler::Jump getJump() const
127 {
128 MacroAssembler::Jump jump;
129 jump.m_label.m_offset = m_codeOffset;
130 #if CPU(ARM_THUMB2)
131 jump.m_type = m_type;
132 jump.m_condition = m_condition;
133 #endif
134 return jump;
135 }
136
137 unsigned m_codeOffset;
138
139 #if CPU(ARM_THUMB2)
140 ARMv7Assembler::JumpType m_type : 8;
141 ARMv7Assembler::Condition m_condition : 8;
142 #endif
143
144 #ifndef NDEBUG
145 enum Mode {
146 InitialJump,
147 LateJump,
148 CorrectedJump
149 };
150
151 Mode m_mode;
152 #endif
153 };
154
155 } } // namespace JSC::DFG
156
157 #endif // ENABLE(DFG_JIT)
158
159 #endif // DFGCorrectableJumpPoint_h