2 * Copyright (C) 2009 University of Szeged
5 * Redistribution and use in source and binary forms, with or without
6 * 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.
14 * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef AssemblerBufferWithConstantPool_h
28 #define AssemblerBufferWithConstantPool_h
30 #include <wtf/Platform.h>
34 #include "AssemblerBuffer.h"
35 #include <wtf/SegmentedVector.h>
37 #define ASSEMBLER_HAS_CONSTANT_POOL 1
42 On a constant pool 4 or 8 bytes data can be stored. The values can be
43 constants or addresses. The addresses should be 32 or 64 bits. The constants
44 should be double-precisions float or integer numbers which are hard to be
45 encoded as few machine instructions.
47 TODO: The pool is desinged to handle both 32 and 64 bits values, but
48 currently only the 4 bytes constants are implemented and tested.
50 The AssemblerBuffer can contain multiple constant pools. Each pool is inserted
51 into the instruction stream - protected by a jump instruction from the
54 The flush mechanism is called when no space remain to insert the next instruction
55 into the pool. Three values are used to determine when the constant pool itself
56 have to be inserted into the instruction stream (Assembler Buffer):
58 - maxPoolSize: size of the constant pool in bytes, this value cannot be
59 larger than the maximum offset of a PC relative memory load
61 - barrierSize: size of jump instruction in bytes which protects the
62 constant pool from execution
64 - maxInstructionSize: maximum length of a machine instruction in bytes
66 There are some callbacks which solve the target architecture specific
69 - TYPE patchConstantPoolLoad(TYPE load, int value):
70 patch the 'load' instruction with the index of the constant in the
71 constant pool and return the patched instruction.
73 - void patchConstantPoolLoad(void* loadAddr, void* constPoolAddr):
74 patch the a PC relative load instruction at 'loadAddr' address with the
75 final relative offset. The offset can be computed with help of
76 'constPoolAddr' (the address of the constant pool) and index of the
77 constant (which is stored previously in the load instruction itself).
79 - TYPE placeConstantPoolBarrier(int size):
80 return with a constant pool barrier instruction which jumps over the
83 The 'put*WithConstant*' functions should be used to place a data into the
87 template <int maxPoolSize
, int barrierSize
, int maxInstructionSize
, class AssemblerType
>
88 class AssemblerBufferWithConstantPool
: public AssemblerBuffer
{
89 typedef SegmentedVector
<uint32_t, 512> LoadOffsets
;
97 AssemblerBufferWithConstantPool()
100 , m_maxDistance(maxPoolSize
)
101 , m_lastConstDelta(0)
103 m_pool
= static_cast<uint32_t*>(fastMalloc(maxPoolSize
));
104 m_mask
= static_cast<char*>(fastMalloc(maxPoolSize
/ sizeof(uint32_t)));
107 ~AssemblerBufferWithConstantPool()
113 void ensureSpace(int space
)
115 flushIfNoSpaceFor(space
);
116 AssemblerBuffer::ensureSpace(space
);
119 void ensureSpace(int insnSpace
, int constSpace
)
121 flushIfNoSpaceFor(insnSpace
, constSpace
);
122 AssemblerBuffer::ensureSpace(insnSpace
);
125 bool isAligned(int alignment
)
127 flushIfNoSpaceFor(alignment
);
128 return AssemblerBuffer::isAligned(alignment
);
131 void putByteUnchecked(int value
)
133 AssemblerBuffer::putByteUnchecked(value
);
137 void putByte(int value
)
139 flushIfNoSpaceFor(1);
140 AssemblerBuffer::putByte(value
);
144 void putShortUnchecked(int value
)
146 AssemblerBuffer::putShortUnchecked(value
);
150 void putShort(int value
)
152 flushIfNoSpaceFor(2);
153 AssemblerBuffer::putShort(value
);
157 void putIntUnchecked(int value
)
159 AssemblerBuffer::putIntUnchecked(value
);
163 void putInt(int value
)
165 flushIfNoSpaceFor(4);
166 AssemblerBuffer::putInt(value
);
170 void putInt64Unchecked(int64_t value
)
172 AssemblerBuffer::putInt64Unchecked(value
);
178 flushIfNoSpaceFor(maxInstructionSize
, sizeof(uint64_t));
179 return AssemblerBuffer::size();
184 return AssemblerBuffer::size();
187 void* executableCopy(ExecutablePool
* allocator
)
189 flushConstantPool(false);
190 return AssemblerBuffer::executableCopy(allocator
);
193 void putIntWithConstantInt(uint32_t insn
, uint32_t constant
, bool isReusable
= false)
195 flushIfNoSpaceFor(4, 4);
197 m_loadOffsets
.append(AssemblerBuffer::size());
199 for (int i
= 0; i
< m_numConsts
; ++i
) {
200 if (m_mask
[i
] == ReusableConst
&& m_pool
[i
] == constant
) {
201 AssemblerBuffer::putInt(AssemblerType::patchConstantPoolLoad(insn
, i
));
207 m_pool
[m_numConsts
] = constant
;
208 m_mask
[m_numConsts
] = static_cast<char>(isReusable
? ReusableConst
: UniqueConst
);
210 AssemblerBuffer::putInt(AssemblerType::patchConstantPoolLoad(insn
, m_numConsts
));
216 // This flushing mechanism can be called after any unconditional jumps.
217 void flushWithoutBarrier(bool isForced
= false)
219 // Flush if constant pool is more than 60% full to avoid overuse of this function.
220 if (isForced
|| 5 * m_numConsts
> 3 * maxPoolSize
/ sizeof(uint32_t))
221 flushConstantPool(false);
224 uint32_t* poolAddress()
229 int sizeOfConstantPool()
235 void correctDeltas(int insnSize
)
237 m_maxDistance
-= insnSize
;
238 m_lastConstDelta
-= insnSize
;
239 if (m_lastConstDelta
< 0)
240 m_lastConstDelta
= 0;
243 void correctDeltas(int insnSize
, int constSize
)
245 correctDeltas(insnSize
);
247 m_maxDistance
-= m_lastConstDelta
;
248 m_lastConstDelta
= constSize
;
251 void flushConstantPool(bool useBarrier
= true)
253 if (m_numConsts
== 0)
255 int alignPool
= (AssemblerBuffer::size() + (useBarrier
? barrierSize
: 0)) & (sizeof(uint64_t) - 1);
258 alignPool
= sizeof(uint64_t) - alignPool
;
260 // Callback to protect the constant pool from execution
262 AssemblerBuffer::putInt(AssemblerType::placeConstantPoolBarrier(m_numConsts
* sizeof(uint32_t) + alignPool
));
266 AssemblerBuffer::putByte(AssemblerType::padForAlign8
);
268 AssemblerBuffer::putShort(AssemblerType::padForAlign16
);
270 AssemblerBuffer::putInt(AssemblerType::padForAlign32
);
273 int constPoolOffset
= AssemblerBuffer::size();
274 append(reinterpret_cast<char*>(m_pool
), m_numConsts
* sizeof(uint32_t));
276 // Patch each PC relative load
277 for (LoadOffsets::Iterator iter
= m_loadOffsets
.begin(); iter
!= m_loadOffsets
.end(); ++iter
) {
278 void* loadAddr
= reinterpret_cast<void*>(m_buffer
+ *iter
);
279 AssemblerType::patchConstantPoolLoad(loadAddr
, reinterpret_cast<void*>(m_buffer
+ constPoolOffset
));
282 m_loadOffsets
.clear();
284 m_maxDistance
= maxPoolSize
;
287 void flushIfNoSpaceFor(int nextInsnSize
)
289 if (m_numConsts
== 0)
291 int lastConstDelta
= m_lastConstDelta
> nextInsnSize
? m_lastConstDelta
- nextInsnSize
: 0;
292 if ((m_maxDistance
< nextInsnSize
+ lastConstDelta
+ barrierSize
+ (int)sizeof(uint32_t)))
296 void flushIfNoSpaceFor(int nextInsnSize
, int nextConstSize
)
298 if (m_numConsts
== 0)
300 if ((m_maxDistance
< nextInsnSize
+ m_lastConstDelta
+ nextConstSize
+ barrierSize
+ (int)sizeof(uint32_t)) ||
301 (m_numConsts
* sizeof(uint32_t) + nextConstSize
>= maxPoolSize
))
307 LoadOffsets m_loadOffsets
;
311 int m_lastConstDelta
;
316 #endif // ENABLE(ASSEMBLER)
318 #endif // AssemblerBufferWithConstantPool_h