]> git.saurik.com Git - apple/javascriptcore.git/blob - jit/JITWriteBarrier.h
10e07100186eeddacf34a3603da6918ec8a42881
[apple/javascriptcore.git] / jit / JITWriteBarrier.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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #ifndef JITWriteBarrier_h
27 #define JITWriteBarrier_h
28
29 #if ENABLE(JIT)
30
31 #include "MacroAssembler.h"
32 #include "MarkStack.h"
33 #include "WriteBarrier.h"
34
35 namespace JSC {
36
37 class JSCell;
38 class JSGlobalData;
39
40 // Needs to be even to appease some of the backends.
41 #define JITWriteBarrierFlag ((void*)2)
42 class JITWriteBarrierBase {
43 public:
44 typedef void* (JITWriteBarrierBase::*UnspecifiedBoolType);
45 operator UnspecifiedBoolType*() const { return get() ? reinterpret_cast<UnspecifiedBoolType*>(1) : 0; }
46 bool operator!() const { return !get(); }
47
48 void setFlagOnBarrier()
49 {
50 ASSERT(!m_location);
51 m_location = CodeLocationDataLabelPtr(JITWriteBarrierFlag);
52 }
53
54 bool isFlagged() const
55 {
56 return !!m_location;
57 }
58
59 void setLocation(CodeLocationDataLabelPtr location)
60 {
61 ASSERT(!m_location);
62 m_location = location;
63 }
64
65 CodeLocationDataLabelPtr location() const
66 {
67 ASSERT((!!m_location) && m_location.executableAddress() != JITWriteBarrierFlag);
68 return m_location;
69 }
70
71 void clear() { clear(0); }
72
73 protected:
74 JITWriteBarrierBase()
75 {
76 }
77
78 void set(JSGlobalData&, CodeLocationDataLabelPtr location, JSCell* owner, JSCell* value)
79 {
80 Heap::writeBarrier(owner, value);
81 m_location = location;
82 ASSERT(((!!m_location) && m_location.executableAddress() != JITWriteBarrierFlag) || (location.executableAddress() == m_location.executableAddress()));
83 MacroAssembler::repatchPointer(m_location, value);
84 ASSERT(get() == value);
85 }
86
87 JSCell* get() const
88 {
89 if (!m_location || m_location.executableAddress() == JITWriteBarrierFlag)
90 return 0;
91 void* result = static_cast<JSCell*>(MacroAssembler::readPointer(m_location));
92 // We use -1 to indicate a "safe" empty value in the instruction stream
93 if (result == (void*)-1)
94 return 0;
95 return static_cast<JSCell*>(result);
96 }
97
98 private:
99 void clear(void* clearedValue)
100 {
101 if (!m_location)
102 return;
103 if (m_location.executableAddress() != JITWriteBarrierFlag)
104 MacroAssembler::repatchPointer(m_location, clearedValue);
105 }
106
107 CodeLocationDataLabelPtr m_location;
108 };
109
110 #undef JITWriteBarrierFlag
111
112 template <typename T> class JITWriteBarrier : public JITWriteBarrierBase {
113 public:
114 JITWriteBarrier()
115 {
116 }
117
118 void set(JSGlobalData& globalData, CodeLocationDataLabelPtr location, JSCell* owner, T* value)
119 {
120 validateCell(owner);
121 validateCell(value);
122 JITWriteBarrierBase::set(globalData, location, owner, value);
123 }
124 T* get() const
125 {
126 T* result = static_cast<T*>(JITWriteBarrierBase::get());
127 if (result)
128 validateCell(result);
129 return result;
130 }
131 };
132
133 template<typename T> inline void MarkStack::append(JITWriteBarrier<T>* slot)
134 {
135 internalAppend(slot->get());
136 }
137
138 }
139
140 #endif // ENABLE(JIT)
141
142 #endif