]>
git.saurik.com Git - apple/javascriptcore.git/blob - assembler/AssemblerBuffer.h
2 * Copyright (C) 2008, 2012, 2014 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
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.
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.
26 #ifndef AssemblerBuffer_h
27 #define AssemblerBuffer_h
31 #include "ExecutableAllocator.h"
32 #include "JITCompilationEffort.h"
35 #include <wtf/Assertions.h>
36 #include <wtf/FastMalloc.h>
37 #include <wtf/StdLibExtras.h>
41 struct AssemblerLabel
{
43 : m_offset(std::numeric_limits
<uint32_t>::max())
47 explicit AssemblerLabel(uint32_t offset
)
52 bool isSet() const { return (m_offset
!= std::numeric_limits
<uint32_t>::max()); }
54 AssemblerLabel
labelAtOffset(int offset
) const
56 return AssemblerLabel(m_offset
+ offset
);
70 AssemblerData(unsigned initialCapacity
)
72 m_capacity
= fastMallocGoodSize(initialCapacity
);
73 m_buffer
= static_cast<char*>(fastMalloc(m_capacity
));
76 AssemblerData(AssemblerData
&& other
)
78 m_buffer
= other
.m_buffer
;
79 other
.m_buffer
= nullptr;
80 m_capacity
= other
.m_capacity
;
84 AssemblerData
& operator=(AssemblerData
&& other
)
86 m_buffer
= other
.m_buffer
;
87 other
.m_buffer
= nullptr;
88 m_capacity
= other
.m_capacity
;
98 char* buffer() const { return m_buffer
; }
100 unsigned capacity() const { return m_capacity
; }
102 void grow(unsigned extraCapacity
= 0)
104 m_capacity
= fastMallocGoodSize(m_capacity
+ m_capacity
/ 2 + extraCapacity
);
105 m_buffer
= static_cast<char*>(fastRealloc(m_buffer
, m_capacity
));
113 class AssemblerBuffer
{
114 static const int initialCapacity
= 128;
117 : m_storage(initialCapacity
)
122 bool isAvailable(int space
)
124 return m_index
+ space
<= m_storage
.capacity();
127 void ensureSpace(int space
)
129 if (!isAvailable(space
))
133 bool isAligned(int alignment
) const
135 return !(m_index
& (alignment
- 1));
138 void putByteUnchecked(int8_t value
) { putIntegralUnchecked(value
); }
139 void putByte(int8_t value
) { putIntegral(value
); }
140 void putShortUnchecked(int16_t value
) { putIntegralUnchecked(value
); }
141 void putShort(int16_t value
) { putIntegral(value
); }
142 void putIntUnchecked(int32_t value
) { putIntegralUnchecked(value
); }
143 void putInt(int32_t value
) { putIntegral(value
); }
144 void putInt64Unchecked(int64_t value
) { putIntegralUnchecked(value
); }
145 void putInt64(int64_t value
) { putIntegral(value
); }
149 return m_storage
.buffer();
152 size_t codeSize() const
157 AssemblerLabel
label() const
159 return AssemblerLabel(m_index
);
162 unsigned debugOffset() { return m_index
; }
164 AssemblerData
releaseAssemblerData() { return WTF::move(m_storage
); }
167 template<typename IntegralType
>
168 void putIntegral(IntegralType value
)
170 unsigned nextIndex
= m_index
+ sizeof(IntegralType
);
171 if (UNLIKELY(nextIndex
> m_storage
.capacity()))
173 ASSERT(isAvailable(sizeof(IntegralType
)));
174 *reinterpret_cast_ptr
<IntegralType
*>(m_storage
.buffer() + m_index
) = value
;
178 template<typename IntegralType
>
179 void putIntegralUnchecked(IntegralType value
)
181 ASSERT(isAvailable(sizeof(IntegralType
)));
182 *reinterpret_cast_ptr
<IntegralType
*>(m_storage
.buffer() + m_index
) = value
;
183 m_index
+= sizeof(IntegralType
);
186 void append(const char* data
, int size
)
188 if (!isAvailable(size
))
191 memcpy(m_storage
.buffer() + m_index
, data
, size
);
195 void grow(int extraCapacity
= 0)
197 m_storage
.grow(extraCapacity
);
201 AssemblerData m_storage
;
207 #endif // ENABLE(ASSEMBLER)
209 #endif // AssemblerBuffer_h