2 * Copyright (C) 2008 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 "JITCompilationEffort.h"
32 #include "JSGlobalData.h"
35 #include <jit/ExecutableAllocator.h>
36 #include <wtf/Assertions.h>
37 #include <wtf/FastMalloc.h>
38 #include <wtf/StdLibExtras.h>
42 struct AssemblerLabel
{
44 : m_offset(std::numeric_limits
<uint32_t>::max())
48 explicit AssemblerLabel(uint32_t offset
)
53 bool isSet() const { return (m_offset
!= std::numeric_limits
<uint32_t>::max()); }
55 AssemblerLabel
labelAtOffset(int offset
) const
57 return AssemblerLabel(m_offset
+ offset
);
63 class AssemblerBuffer
{
64 static const int inlineCapacity
= 128;
67 : m_storage(inlineCapacity
)
68 , m_buffer(m_storage
.begin())
69 , m_capacity(inlineCapacity
)
78 bool isAvailable(int space
)
80 return m_index
+ space
<= m_capacity
;
83 void ensureSpace(int space
)
85 if (!isAvailable(space
))
89 bool isAligned(int alignment
) const
91 return !(m_index
& (alignment
- 1));
94 template<typename IntegralType
>
95 void putIntegral(IntegralType value
)
97 ensureSpace(sizeof(IntegralType
));
98 putIntegralUnchecked(value
);
101 template<typename IntegralType
>
102 void putIntegralUnchecked(IntegralType value
)
104 ASSERT(isAvailable(sizeof(IntegralType
)));
105 *reinterpret_cast_ptr
<IntegralType
*>(m_buffer
+ m_index
) = value
;
106 m_index
+= sizeof(IntegralType
);
109 void putByteUnchecked(int8_t value
) { putIntegralUnchecked(value
); }
110 void putByte(int8_t value
) { putIntegral(value
); }
111 void putShortUnchecked(int16_t value
) { putIntegralUnchecked(value
); }
112 void putShort(int16_t value
) { putIntegral(value
); }
113 void putIntUnchecked(int32_t value
) { putIntegralUnchecked(value
); }
114 void putInt(int32_t value
) { putIntegral(value
); }
115 void putInt64Unchecked(int64_t value
) { putIntegralUnchecked(value
); }
116 void putInt64(int64_t value
) { putIntegral(value
); }
123 size_t codeSize() const
128 AssemblerLabel
label() const
130 return AssemblerLabel(m_index
);
133 PassRefPtr
<ExecutableMemoryHandle
> executableCopy(JSGlobalData
& globalData
, void* ownerUID
, JITCompilationEffort effort
)
138 RefPtr
<ExecutableMemoryHandle
> result
= globalData
.executableAllocator
.allocate(globalData
, m_index
, ownerUID
, effort
);
143 ExecutableAllocator::makeWritable(result
->start(), result
->sizeInBytes());
145 memcpy(result
->start(), m_buffer
, m_index
);
147 return result
.release();
150 unsigned debugOffset() { return m_index
; }
153 void append(const char* data
, int size
)
155 if (!isAvailable(size
))
158 memcpy(m_buffer
+ m_index
, data
, size
);
162 void grow(int extraCapacity
= 0)
164 m_capacity
+= m_capacity
/ 2 + extraCapacity
;
166 m_storage
.grow(m_capacity
);
167 m_buffer
= m_storage
.begin();
171 Vector
<char, inlineCapacity
> m_storage
;
179 #endif // ENABLE(ASSEMBLER)
181 #endif // AssemblerBuffer_h