AssemblerBufferWithConstantPool.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (C) 2009 University of Szeged
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  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. *
  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.
  25. */
  26. #ifndef AssemblerBufferWithConstantPool_h
  27. #define AssemblerBufferWithConstantPool_h
  28. #if ENABLE(ASSEMBLER)
  29. #include "AssemblerBuffer.h"
  30. #include <wtf/SegmentedVector.h>
  31. #define ASSEMBLER_HAS_CONSTANT_POOL 1
  32. namespace JSC {
  33. /*
  34. On a constant pool 4 or 8 bytes data can be stored. The values can be
  35. constants or addresses. The addresses should be 32 or 64 bits. The constants
  36. should be double-precisions float or integer numbers which are hard to be
  37. encoded as few machine instructions.
  38. TODO: The pool is desinged to handle both 32 and 64 bits values, but
  39. currently only the 4 bytes constants are implemented and tested.
  40. The AssemblerBuffer can contain multiple constant pools. Each pool is inserted
  41. into the instruction stream - protected by a jump instruction from the
  42. execution flow.
  43. The flush mechanism is called when no space remain to insert the next instruction
  44. into the pool. Three values are used to determine when the constant pool itself
  45. have to be inserted into the instruction stream (Assembler Buffer):
  46. - maxPoolSize: size of the constant pool in bytes, this value cannot be
  47. larger than the maximum offset of a PC relative memory load
  48. - barrierSize: size of jump instruction in bytes which protects the
  49. constant pool from execution
  50. - maxInstructionSize: maximum length of a machine instruction in bytes
  51. There are some callbacks which solve the target architecture specific
  52. address handling:
  53. - TYPE patchConstantPoolLoad(TYPE load, int value):
  54. patch the 'load' instruction with the index of the constant in the
  55. constant pool and return the patched instruction.
  56. - void patchConstantPoolLoad(void* loadAddr, void* constPoolAddr):
  57. patch the a PC relative load instruction at 'loadAddr' address with the
  58. final relative offset. The offset can be computed with help of
  59. 'constPoolAddr' (the address of the constant pool) and index of the
  60. constant (which is stored previously in the load instruction itself).
  61. - TYPE placeConstantPoolBarrier(int size):
  62. return with a constant pool barrier instruction which jumps over the
  63. constant pool.
  64. The 'put*WithConstant*' functions should be used to place a data into the
  65. constant pool.
  66. */
  67. template <int maxPoolSize, int barrierSize, int maxInstructionSize, class AssemblerType>
  68. class AssemblerBufferWithConstantPool : public AssemblerBuffer {
  69. typedef SegmentedVector<uint32_t, 512> LoadOffsets;
  70. using AssemblerBuffer::putIntegral;
  71. using AssemblerBuffer::putIntegralUnchecked;
  72. public:
  73. typedef struct {
  74. short high;
  75. short low;
  76. } TwoShorts;
  77. enum {
  78. UniqueConst,
  79. ReusableConst,
  80. UnusedEntry,
  81. };
  82. AssemblerBufferWithConstantPool()
  83. : AssemblerBuffer()
  84. , m_numConsts(0)
  85. , m_maxDistance(maxPoolSize)
  86. , m_lastConstDelta(0)
  87. {
  88. m_pool = static_cast<uint32_t*>(fastMalloc(maxPoolSize));
  89. m_mask = static_cast<char*>(fastMalloc(maxPoolSize / sizeof(uint32_t)));
  90. }
  91. ~AssemblerBufferWithConstantPool()
  92. {
  93. fastFree(m_mask);
  94. fastFree(m_pool);
  95. }
  96. void ensureSpace(int space)
  97. {
  98. flushIfNoSpaceFor(space);
  99. AssemblerBuffer::ensureSpace(space);
  100. }
  101. void ensureSpace(int insnSpace, int constSpace)
  102. {
  103. flushIfNoSpaceFor(insnSpace, constSpace);
  104. AssemblerBuffer::ensureSpace(insnSpace);
  105. }
  106. void ensureSpaceForAnyInstruction(int amount = 1)
  107. {
  108. flushIfNoSpaceFor(amount * maxInstructionSize, amount * sizeof(uint64_t));
  109. }
  110. bool isAligned(int alignment)
  111. {
  112. flushIfNoSpaceFor(alignment);
  113. return AssemblerBuffer::isAligned(alignment);
  114. }
  115. void putByteUnchecked(int value)
  116. {
  117. AssemblerBuffer::putByteUnchecked(value);
  118. correctDeltas(1);
  119. }
  120. void putByte(int value)
  121. {
  122. flushIfNoSpaceFor(1);
  123. AssemblerBuffer::putByte(value);
  124. correctDeltas(1);
  125. }
  126. void putShortUnchecked(int value)
  127. {
  128. AssemblerBuffer::putShortUnchecked(value);
  129. correctDeltas(2);
  130. }
  131. void putShort(int value)
  132. {
  133. flushIfNoSpaceFor(2);
  134. AssemblerBuffer::putShort(value);
  135. correctDeltas(2);
  136. }
  137. void putIntUnchecked(int value)
  138. {
  139. AssemblerBuffer::putIntUnchecked(value);
  140. correctDeltas(4);
  141. }
  142. void putInt(int value)
  143. {
  144. flushIfNoSpaceFor(4);
  145. AssemblerBuffer::putInt(value);
  146. correctDeltas(4);
  147. }
  148. void putInt64Unchecked(int64_t value)
  149. {
  150. AssemblerBuffer::putInt64Unchecked(value);
  151. correctDeltas(8);
  152. }
  153. void putIntegral(TwoShorts value)
  154. {
  155. putIntegral(value.high);
  156. putIntegral(value.low);
  157. }
  158. void putIntegralUnchecked(TwoShorts value)
  159. {
  160. putIntegralUnchecked(value.high);
  161. putIntegralUnchecked(value.low);
  162. }
  163. PassRefPtr<ExecutableMemoryHandle> executableCopy(VM& vm, void* ownerUID, JITCompilationEffort effort)
  164. {
  165. flushConstantPool(false);
  166. return AssemblerBuffer::executableCopy(vm, ownerUID, effort);
  167. }
  168. void putShortWithConstantInt(uint16_t insn, uint32_t constant, bool isReusable = false)
  169. {
  170. putIntegralWithConstantInt(insn, constant, isReusable);
  171. }
  172. void putIntWithConstantInt(uint32_t insn, uint32_t constant, bool isReusable = false)
  173. {
  174. putIntegralWithConstantInt(insn, constant, isReusable);
  175. }
  176. // This flushing mechanism can be called after any unconditional jumps.
  177. void flushWithoutBarrier(bool isForced = false)
  178. {
  179. // Flush if constant pool is more than 60% full to avoid overuse of this function.
  180. if (isForced || 5 * static_cast<uint32_t>(m_numConsts) > 3 * maxPoolSize / sizeof(uint32_t))
  181. flushConstantPool(false);
  182. }
  183. uint32_t* poolAddress()
  184. {
  185. return m_pool;
  186. }
  187. int sizeOfConstantPool()
  188. {
  189. return m_numConsts;
  190. }
  191. private:
  192. void correctDeltas(int insnSize)
  193. {
  194. m_maxDistance -= insnSize;
  195. m_lastConstDelta -= insnSize;
  196. if (m_lastConstDelta < 0)
  197. m_lastConstDelta = 0;
  198. }
  199. void correctDeltas(int insnSize, int constSize)
  200. {
  201. correctDeltas(insnSize);
  202. m_maxDistance -= m_lastConstDelta;
  203. m_lastConstDelta = constSize;
  204. }
  205. template<typename IntegralType>
  206. void putIntegralWithConstantInt(IntegralType insn, uint32_t constant, bool isReusable)
  207. {
  208. if (!m_numConsts)
  209. m_maxDistance = maxPoolSize;
  210. flushIfNoSpaceFor(sizeof(IntegralType), 4);
  211. m_loadOffsets.append(codeSize());
  212. if (isReusable) {
  213. for (int i = 0; i < m_numConsts; ++i) {
  214. if (m_mask[i] == ReusableConst && m_pool[i] == constant) {
  215. putIntegral(static_cast<IntegralType>(AssemblerType::patchConstantPoolLoad(insn, i)));
  216. correctDeltas(sizeof(IntegralType));
  217. return;
  218. }
  219. }
  220. }
  221. m_pool[m_numConsts] = constant;
  222. m_mask[m_numConsts] = static_cast<char>(isReusable ? ReusableConst : UniqueConst);
  223. putIntegral(static_cast<IntegralType>(AssemblerType::patchConstantPoolLoad(insn, m_numConsts)));
  224. ++m_numConsts;
  225. correctDeltas(sizeof(IntegralType), 4);
  226. }
  227. void flushConstantPool(bool useBarrier = true)
  228. {
  229. if (m_numConsts == 0)
  230. return;
  231. int alignPool = (codeSize() + (useBarrier ? barrierSize : 0)) & (sizeof(uint64_t) - 1);
  232. if (alignPool)
  233. alignPool = sizeof(uint64_t) - alignPool;
  234. // Callback to protect the constant pool from execution
  235. if (useBarrier)
  236. putIntegral(AssemblerType::placeConstantPoolBarrier(m_numConsts * sizeof(uint32_t) + alignPool));
  237. if (alignPool) {
  238. if (alignPool & 1)
  239. AssemblerBuffer::putByte(AssemblerType::padForAlign8);
  240. if (alignPool & 2)
  241. AssemblerBuffer::putShort(AssemblerType::padForAlign16);
  242. if (alignPool & 4)
  243. AssemblerBuffer::putInt(AssemblerType::padForAlign32);
  244. }
  245. int constPoolOffset = codeSize();
  246. append(reinterpret_cast<char*>(m_pool), m_numConsts * sizeof(uint32_t));
  247. // Patch each PC relative load
  248. for (LoadOffsets::Iterator iter = m_loadOffsets.begin(); iter != m_loadOffsets.end(); ++iter) {
  249. void* loadAddr = reinterpret_cast<char*>(data()) + *iter;
  250. AssemblerType::patchConstantPoolLoad(loadAddr, reinterpret_cast<char*>(data()) + constPoolOffset);
  251. }
  252. m_loadOffsets.clear();
  253. m_numConsts = 0;
  254. }
  255. void flushIfNoSpaceFor(int nextInsnSize)
  256. {
  257. if (m_numConsts == 0)
  258. return;
  259. int lastConstDelta = m_lastConstDelta > nextInsnSize ? m_lastConstDelta - nextInsnSize : 0;
  260. if ((m_maxDistance < nextInsnSize + lastConstDelta + barrierSize + (int)sizeof(uint32_t)))
  261. flushConstantPool();
  262. }
  263. void flushIfNoSpaceFor(int nextInsnSize, int nextConstSize)
  264. {
  265. if (m_numConsts == 0)
  266. return;
  267. if ((m_maxDistance < nextInsnSize + m_lastConstDelta + nextConstSize + barrierSize + (int)sizeof(uint32_t)) ||
  268. (m_numConsts * sizeof(uint32_t) + nextConstSize >= maxPoolSize))
  269. flushConstantPool();
  270. }
  271. uint32_t* m_pool;
  272. char* m_mask;
  273. LoadOffsets m_loadOffsets;
  274. int m_numConsts;
  275. int m_maxDistance;
  276. int m_lastConstDelta;
  277. };
  278. } // namespace JSC
  279. #endif // ENABLE(ASSEMBLER)
  280. #endif // AssemblerBufferWithConstantPool_h