2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifndef CompactJITCodeMap_h
30 #define CompactJITCodeMap_h
32 #include <wtf/Assertions.h>
33 #include <wtf/FastMalloc.h>
34 #include <wtf/FastMalloc.h>
35 #include <wtf/OwnPtr.h>
36 #include <wtf/PassOwnPtr.h>
37 #include <wtf/Vector.h>
41 // Gives you a compressed map between between bytecode indices and machine code
42 // entry points. The compression simply tries to use either 1, 2, or 4 bytes for
43 // any given offset. The largest offset that can be stored is 2^30.
47 // CompactJITCodeMap::Encoder encoder(map);
48 // encoder.append(a, b);
49 // encoder.append(c, d); // preconditions: c >= a, d >= b
50 // OwnPtr<CompactJITCodeMap> map = encoder.finish();
52 // At some later time:
54 // Vector<BytecodeAndMachineOffset> decoded;
55 // map->decode(decoded);
57 struct BytecodeAndMachineOffset
{
58 BytecodeAndMachineOffset() { }
60 BytecodeAndMachineOffset(unsigned bytecodeIndex
, unsigned machineCodeOffset
)
61 : m_bytecodeIndex(bytecodeIndex
)
62 , m_machineCodeOffset(machineCodeOffset
)
66 unsigned m_bytecodeIndex
;
67 unsigned m_machineCodeOffset
;
69 static inline unsigned getBytecodeIndex(BytecodeAndMachineOffset
* mapping
)
71 return mapping
->m_bytecodeIndex
;
74 static inline unsigned getMachineCodeOffset(BytecodeAndMachineOffset
* mapping
)
76 return mapping
->m_machineCodeOffset
;
80 class CompactJITCodeMap
{
81 WTF_MAKE_FAST_ALLOCATED
;
89 unsigned numberOfEntries() const
91 return m_numberOfEntries
;
94 void decode(Vector
<BytecodeAndMachineOffset
>& result
) const;
97 CompactJITCodeMap(uint8_t* buffer
, unsigned size
, unsigned numberOfEntries
)
102 , m_numberOfEntries(numberOfEntries
)
107 uint8_t at(unsigned index
) const
109 ASSERT(index
< m_size
);
110 return m_buffer
[index
];
113 unsigned decodeNumber(unsigned& index
) const
115 uint8_t headValue
= at(index
++);
116 if (!(headValue
& 128))
118 if (!(headValue
& 64))
119 return (static_cast<unsigned>(headValue
& ~128) << 8) | at(index
++);
120 unsigned second
= at(index
++);
121 unsigned third
= at(index
++);
122 unsigned fourth
= at(index
++);
123 return (static_cast<unsigned>(headValue
& ~(128 + 64)) << 24) | (second
<< 16) | (third
<< 8) | fourth
;
130 unsigned m_numberOfEntries
;
134 WTF_MAKE_NONCOPYABLE(Encoder
);
139 void ensureCapacityFor(unsigned numberOfEntriesToAdd
);
140 void append(unsigned bytecodeIndex
, unsigned machineCodeOffset
);
141 PassOwnPtr
<CompactJITCodeMap
> finish();
144 void appendByte(uint8_t value
);
145 void encodeNumber(uint32_t value
);
150 unsigned m_numberOfEntries
;
152 unsigned m_previousBytecodeIndex
;
153 unsigned m_previousMachineCodeOffset
;
157 WTF_MAKE_NONCOPYABLE(Decoder
);
159 Decoder(const CompactJITCodeMap
*);
161 unsigned numberOfEntriesRemaining() const;
162 void read(unsigned& bytecodeIndex
, unsigned& machineCodeOffset
);
165 const CompactJITCodeMap
* m_jitCodeMap
;
166 unsigned m_previousBytecodeIndex
;
167 unsigned m_previousMachineCodeOffset
;
168 unsigned m_numberOfEntriesRemaining
;
169 unsigned m_bufferIndex
;
173 friend class Encoder
;
174 friend class Decoder
;
177 inline void CompactJITCodeMap::decode(Vector
<BytecodeAndMachineOffset
>& result
) const
179 Decoder
decoder(this);
180 result
.resize(decoder
.numberOfEntriesRemaining());
181 for (unsigned i
= 0; i
< result
.size(); ++i
)
182 decoder
.read(result
[i
].m_bytecodeIndex
, result
[i
].m_machineCodeOffset
);
184 ASSERT(!decoder
.numberOfEntriesRemaining());
187 inline CompactJITCodeMap::Encoder::Encoder()
191 , m_numberOfEntries(0)
192 , m_previousBytecodeIndex(0)
193 , m_previousMachineCodeOffset(0)
197 inline CompactJITCodeMap::Encoder::~Encoder()
203 inline void CompactJITCodeMap::Encoder::append(unsigned bytecodeIndex
, unsigned machineCodeOffset
)
205 ASSERT(bytecodeIndex
>= m_previousBytecodeIndex
);
206 ASSERT(machineCodeOffset
>= m_previousMachineCodeOffset
);
207 ensureCapacityFor(1);
208 encodeNumber(bytecodeIndex
- m_previousBytecodeIndex
);
209 encodeNumber(machineCodeOffset
- m_previousMachineCodeOffset
);
210 m_previousBytecodeIndex
= bytecodeIndex
;
211 m_previousMachineCodeOffset
= machineCodeOffset
;
215 inline PassOwnPtr
<CompactJITCodeMap
> CompactJITCodeMap::Encoder::finish()
218 m_buffer
= static_cast<uint8_t*>(fastRealloc(m_buffer
, m_capacity
));
219 OwnPtr
<CompactJITCodeMap
> result
= adoptPtr(new CompactJITCodeMap(m_buffer
, m_size
, m_numberOfEntries
));
223 m_numberOfEntries
= 0;
224 m_previousBytecodeIndex
= 0;
225 m_previousMachineCodeOffset
= 0;
226 return result
.release();
229 inline void CompactJITCodeMap::Encoder::appendByte(uint8_t value
)
231 ASSERT(m_size
+ 1 <= m_capacity
);
232 m_buffer
[m_size
++] = value
;
235 inline void CompactJITCodeMap::Encoder::encodeNumber(uint32_t value
)
237 ASSERT(m_size
+ 4 <= m_capacity
);
238 ASSERT(value
< (1 << 30));
240 uint8_t headValue
= static_cast<uint8_t>(value
);
241 ASSERT(!(headValue
& 128));
242 appendByte(headValue
);
243 } else if (value
<= 16383) {
244 uint8_t headValue
= static_cast<uint8_t>(value
>> 8);
245 ASSERT(!(headValue
& 128));
246 ASSERT(!(headValue
& 64));
247 appendByte(headValue
| 128);
248 appendByte(static_cast<uint8_t>(value
));
250 uint8_t headValue
= static_cast<uint8_t>(value
>> 24);
251 ASSERT(!(headValue
& 128));
252 ASSERT(!(headValue
& 64));
253 appendByte(headValue
| 128 | 64);
254 appendByte(static_cast<uint8_t>(value
>> 16));
255 appendByte(static_cast<uint8_t>(value
>> 8));
256 appendByte(static_cast<uint8_t>(value
));
260 inline void CompactJITCodeMap::Encoder::ensureCapacityFor(unsigned numberOfEntriesToAdd
)
262 unsigned capacityNeeded
= m_size
+ numberOfEntriesToAdd
* 2 * 4;
263 if (capacityNeeded
> m_capacity
) {
264 m_capacity
= capacityNeeded
* 2;
265 m_buffer
= static_cast<uint8_t*>(fastRealloc(m_buffer
, m_capacity
));
269 inline CompactJITCodeMap::Decoder::Decoder(const CompactJITCodeMap
* jitCodeMap
)
270 : m_jitCodeMap(jitCodeMap
)
271 , m_previousBytecodeIndex(0)
272 , m_previousMachineCodeOffset(0)
273 , m_numberOfEntriesRemaining(jitCodeMap
->m_numberOfEntries
)
278 inline unsigned CompactJITCodeMap::Decoder::numberOfEntriesRemaining() const
280 ASSERT(m_numberOfEntriesRemaining
|| m_bufferIndex
== m_jitCodeMap
->m_size
);
281 return m_numberOfEntriesRemaining
;
284 inline void CompactJITCodeMap::Decoder::read(unsigned& bytecodeIndex
, unsigned& machineCodeOffset
)
286 ASSERT(numberOfEntriesRemaining());
288 m_previousBytecodeIndex
+= m_jitCodeMap
->decodeNumber(m_bufferIndex
);
289 m_previousMachineCodeOffset
+= m_jitCodeMap
->decodeNumber(m_bufferIndex
);
290 bytecodeIndex
= m_previousBytecodeIndex
;
291 machineCodeOffset
= m_previousMachineCodeOffset
;
292 m_numberOfEntriesRemaining
--;
297 #endif // CompactJITCodeMap_h