AssemblerBuffer.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2008, 2012 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. #ifndef AssemblerBuffer_h
  26. #define AssemblerBuffer_h
  27. #if ENABLE(ASSEMBLER)
  28. #include "ExecutableAllocator.h"
  29. #include "JITCompilationEffort.h"
  30. #include "VM.h"
  31. #include "stdint.h"
  32. #include <string.h>
  33. #include <wtf/Assertions.h>
  34. #include <wtf/FastMalloc.h>
  35. #include <wtf/StdLibExtras.h>
  36. namespace JSC {
  37. struct AssemblerLabel {
  38. AssemblerLabel()
  39. : m_offset(std::numeric_limits<uint32_t>::max())
  40. {
  41. }
  42. explicit AssemblerLabel(uint32_t offset)
  43. : m_offset(offset)
  44. {
  45. }
  46. bool isSet() const { return (m_offset != std::numeric_limits<uint32_t>::max()); }
  47. AssemblerLabel labelAtOffset(int offset) const
  48. {
  49. return AssemblerLabel(m_offset + offset);
  50. }
  51. uint32_t m_offset;
  52. };
  53. class AssemblerBuffer {
  54. static const int inlineCapacity = 128;
  55. public:
  56. AssemblerBuffer()
  57. : m_storage(inlineCapacity)
  58. , m_buffer(m_storage.begin())
  59. , m_capacity(inlineCapacity)
  60. , m_index(0)
  61. {
  62. }
  63. ~AssemblerBuffer()
  64. {
  65. }
  66. bool isAvailable(int space)
  67. {
  68. return m_index + space <= m_capacity;
  69. }
  70. void ensureSpace(int space)
  71. {
  72. if (!isAvailable(space))
  73. grow();
  74. }
  75. bool isAligned(int alignment) const
  76. {
  77. return !(m_index & (alignment - 1));
  78. }
  79. template<typename IntegralType>
  80. void putIntegral(IntegralType value)
  81. {
  82. ensureSpace(sizeof(IntegralType));
  83. putIntegralUnchecked(value);
  84. }
  85. template<typename IntegralType>
  86. void putIntegralUnchecked(IntegralType value)
  87. {
  88. ASSERT(isAvailable(sizeof(IntegralType)));
  89. *reinterpret_cast_ptr<IntegralType*>(m_buffer + m_index) = value;
  90. m_index += sizeof(IntegralType);
  91. }
  92. void putByteUnchecked(int8_t value) { putIntegralUnchecked(value); }
  93. void putByte(int8_t value) { putIntegral(value); }
  94. void putShortUnchecked(int16_t value) { putIntegralUnchecked(value); }
  95. void putShort(int16_t value) { putIntegral(value); }
  96. void putIntUnchecked(int32_t value) { putIntegralUnchecked(value); }
  97. void putInt(int32_t value) { putIntegral(value); }
  98. void putInt64Unchecked(int64_t value) { putIntegralUnchecked(value); }
  99. void putInt64(int64_t value) { putIntegral(value); }
  100. void* data() const
  101. {
  102. return m_buffer;
  103. }
  104. size_t codeSize() const
  105. {
  106. return m_index;
  107. }
  108. AssemblerLabel label() const
  109. {
  110. return AssemblerLabel(m_index);
  111. }
  112. PassRefPtr<ExecutableMemoryHandle> executableCopy(VM& vm, void* ownerUID, JITCompilationEffort effort)
  113. {
  114. if (!m_index)
  115. return 0;
  116. RefPtr<ExecutableMemoryHandle> result = vm.executableAllocator.allocate(vm, m_index, ownerUID, effort);
  117. if (!result)
  118. return 0;
  119. ExecutableAllocator::makeWritable(result->start(), result->sizeInBytes());
  120. memcpy(result->start(), m_buffer, m_index);
  121. return result.release();
  122. }
  123. unsigned debugOffset() { return m_index; }
  124. protected:
  125. void append(const char* data, int size)
  126. {
  127. if (!isAvailable(size))
  128. grow(size);
  129. memcpy(m_buffer + m_index, data, size);
  130. m_index += size;
  131. }
  132. void grow(int extraCapacity = 0)
  133. {
  134. m_capacity += m_capacity / 2 + extraCapacity;
  135. m_storage.grow(m_capacity);
  136. m_buffer = m_storage.begin();
  137. }
  138. private:
  139. Vector<char, inlineCapacity, UnsafeVectorOverflow> m_storage;
  140. char* m_buffer;
  141. int m_capacity;
  142. int m_index;
  143. };
  144. } // namespace JSC
  145. #endif // ENABLE(ASSEMBLER)
  146. #endif // AssemblerBuffer_h