]> git.saurik.com Git - apple/javascriptcore.git/blame - assembler/AssemblerBuffer.h
JavaScriptCore-903.tar.gz
[apple/javascriptcore.git] / assembler / AssemblerBuffer.h
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 2008 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
26#ifndef AssemblerBuffer_h
27#define AssemblerBuffer_h
28
9dae56ea
A
29#if ENABLE(ASSEMBLER)
30
31#include "stdint.h"
32#include <string.h>
33#include <jit/ExecutableAllocator.h>
34#include <wtf/Assertions.h>
35#include <wtf/FastMalloc.h>
14957cd0 36#include <wtf/StdLibExtras.h>
9dae56ea
A
37
38namespace JSC {
39
14957cd0
A
40 struct AssemblerLabel {
41 AssemblerLabel()
42 : m_offset(std::numeric_limits<uint32_t>::max())
9dae56ea
A
43 {
44 }
45
14957cd0
A
46 explicit AssemblerLabel(uint32_t offset)
47 : m_offset(offset)
9dae56ea 48 {
9dae56ea
A
49 }
50
14957cd0 51 bool isSet() const { return (m_offset != std::numeric_limits<uint32_t>::max()); }
9dae56ea 52
14957cd0 53 AssemblerLabel labelAtOffset(int offset) const
9dae56ea 54 {
14957cd0 55 return AssemblerLabel(m_offset + offset);
9dae56ea
A
56 }
57
14957cd0
A
58 uint32_t m_offset;
59 };
60
61 class AssemblerBuffer {
62 static const int inlineCapacity = 128;
63 public:
64 AssemblerBuffer()
65 : m_storage(inlineCapacity)
66 , m_buffer(m_storage.begin())
67 , m_capacity(inlineCapacity)
68 , m_index(0)
9dae56ea 69 {
9dae56ea
A
70 }
71
14957cd0 72 ~AssemblerBuffer()
9dae56ea 73 {
9dae56ea
A
74 }
75
14957cd0 76 bool isAvailable(int space)
9dae56ea 77 {
14957cd0 78 return m_index + space <= m_capacity;
9dae56ea
A
79 }
80
14957cd0 81 void ensureSpace(int space)
9dae56ea 82 {
14957cd0 83 if (!isAvailable(space))
9dae56ea 84 grow();
9dae56ea
A
85 }
86
14957cd0 87 bool isAligned(int alignment) const
9dae56ea 88 {
14957cd0 89 return !(m_index & (alignment - 1));
9dae56ea
A
90 }
91
14957cd0
A
92 template<typename IntegralType>
93 void putIntegral(IntegralType value)
9dae56ea 94 {
14957cd0
A
95 ensureSpace(sizeof(IntegralType));
96 putIntegralUnchecked(value);
9dae56ea
A
97 }
98
14957cd0
A
99 template<typename IntegralType>
100 void putIntegralUnchecked(IntegralType value)
9dae56ea 101 {
14957cd0
A
102 ASSERT(isAvailable(sizeof(IntegralType)));
103 *reinterpret_cast_ptr<IntegralType*>(m_buffer + m_index) = value;
104 m_index += sizeof(IntegralType);
9dae56ea
A
105 }
106
14957cd0
A
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); }
115
9dae56ea
A
116 void* data() const
117 {
118 return m_buffer;
119 }
120
14957cd0 121 size_t codeSize() const
9dae56ea 122 {
14957cd0 123 return m_index;
9dae56ea
A
124 }
125
14957cd0 126 AssemblerLabel label() const
9dae56ea 127 {
14957cd0
A
128 return AssemblerLabel(m_index);
129 }
130
131 void* executableCopy(JSGlobalData& globalData, ExecutablePool* allocator)
132 {
133 if (!m_index)
9dae56ea
A
134 return 0;
135
14957cd0 136 void* result = allocator->alloc(globalData, m_index);
9dae56ea
A
137
138 if (!result)
139 return 0;
140
14957cd0 141 ExecutableAllocator::makeWritable(result, m_index);
ba379fdc 142
14957cd0 143 return memcpy(result, m_buffer, m_index);
9dae56ea
A
144 }
145
14957cd0
A
146 void rewindToLabel(AssemblerLabel label)
147 {
148 m_index = label.m_offset;
149 }
150
151#ifndef NDEBUG
152 unsigned debugOffset() { return m_index; }
153#endif
154
ba379fdc
A
155 protected:
156 void append(const char* data, int size)
157 {
14957cd0 158 if (!isAvailable(size))
ba379fdc
A
159 grow(size);
160
14957cd0
A
161 memcpy(m_buffer + m_index, data, size);
162 m_index += size;
ba379fdc
A
163 }
164
165 void grow(int extraCapacity = 0)
9dae56ea 166 {
ba379fdc 167 m_capacity += m_capacity / 2 + extraCapacity;
9dae56ea 168
14957cd0
A
169 m_storage.grow(m_capacity);
170 m_buffer = m_storage.begin();
9dae56ea
A
171 }
172
14957cd0
A
173 private:
174 Vector<char, inlineCapacity> m_storage;
9dae56ea
A
175 char* m_buffer;
176 int m_capacity;
14957cd0 177 int m_index;
9dae56ea
A
178 };
179
180} // namespace JSC
181
182#endif // ENABLE(ASSEMBLER)
183
184#endif // AssemblerBuffer_h