]>
git.saurik.com Git - apple/javascriptcore.git/blob - assembler/AssemblerBuffer.h
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
33 #include <jit/ExecutableAllocator.h>
34 #include <wtf/Assertions.h>
35 #include <wtf/FastMalloc.h>
36 #include <wtf/StdLibExtras.h>
40 struct AssemblerLabel
{
42 : m_offset(std::numeric_limits
<uint32_t>::max())
46 explicit AssemblerLabel(uint32_t offset
)
51 bool isSet() const { return (m_offset
!= std::numeric_limits
<uint32_t>::max()); }
53 AssemblerLabel
labelAtOffset(int offset
) const
55 return AssemblerLabel(m_offset
+ offset
);
61 class AssemblerBuffer
{
62 static const int inlineCapacity
= 128;
65 : m_storage(inlineCapacity
)
66 , m_buffer(m_storage
.begin())
67 , m_capacity(inlineCapacity
)
76 bool isAvailable(int space
)
78 return m_index
+ space
<= m_capacity
;
81 void ensureSpace(int space
)
83 if (!isAvailable(space
))
87 bool isAligned(int alignment
) const
89 return !(m_index
& (alignment
- 1));
92 template<typename IntegralType
>
93 void putIntegral(IntegralType value
)
95 ensureSpace(sizeof(IntegralType
));
96 putIntegralUnchecked(value
);
99 template<typename IntegralType
>
100 void putIntegralUnchecked(IntegralType value
)
102 ASSERT(isAvailable(sizeof(IntegralType
)));
103 *reinterpret_cast_ptr
<IntegralType
*>(m_buffer
+ m_index
) = value
;
104 m_index
+= sizeof(IntegralType
);
107 void putByteUnchecked(int8_t value
) { putIntegralUnchecked(value
); }
108 void putByte(int8_t value
) { putIntegral(value
); }
109 void putShortUnchecked(int16_t value
) { putIntegralUnchecked(value
); }
110 void putShort(int16_t value
) { putIntegral(value
); }
111 void putIntUnchecked(int32_t value
) { putIntegralUnchecked(value
); }
112 void putInt(int32_t value
) { putIntegral(value
); }
113 void putInt64Unchecked(int64_t value
) { putIntegralUnchecked(value
); }
114 void putInt64(int64_t value
) { putIntegral(value
); }
121 size_t codeSize() const
126 AssemblerLabel
label() const
128 return AssemblerLabel(m_index
);
131 void* executableCopy(JSGlobalData
& globalData
, ExecutablePool
* allocator
)
136 void* result
= allocator
->alloc(globalData
, m_index
);
141 ExecutableAllocator::makeWritable(result
, m_index
);
143 return memcpy(result
, m_buffer
, m_index
);
146 void rewindToLabel(AssemblerLabel label
)
148 m_index
= label
.m_offset
;
152 unsigned debugOffset() { return m_index
; }
156 void append(const char* data
, int size
)
158 if (!isAvailable(size
))
161 memcpy(m_buffer
+ m_index
, data
, size
);
165 void grow(int extraCapacity
= 0)
167 m_capacity
+= m_capacity
/ 2 + extraCapacity
;
169 m_storage
.grow(m_capacity
);
170 m_buffer
= m_storage
.begin();
174 Vector
<char, inlineCapacity
> m_storage
;
182 #endif // ENABLE(ASSEMBLER)
184 #endif // AssemblerBuffer_h