]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2008, 2009 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 | * | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | |
14 | * its contributors may be used to endorse or promote products derived | |
15 | * from this software without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #ifndef RegisterFile_h | |
30 | #define RegisterFile_h | |
31 | ||
32 | #include "Collector.h" | |
33 | #include "ExecutableAllocator.h" | |
34 | #include "Register.h" | |
35 | #include "WeakGCPtr.h" | |
36 | #include <stdio.h> | |
37 | #include <wtf/Noncopyable.h> | |
38 | #include <wtf/VMTags.h> | |
39 | ||
40 | #if HAVE(MMAP) | |
41 | #include <errno.h> | |
42 | #include <sys/mman.h> | |
43 | #endif | |
44 | ||
45 | namespace JSC { | |
46 | ||
47 | /* | |
48 | A register file is a stack of register frames. We represent a register | |
49 | frame by its offset from "base", the logical first entry in the register | |
50 | file. The bottom-most register frame's offset from base is 0. | |
51 | ||
52 | In a program where function "a" calls function "b" (global code -> a -> b), | |
53 | the register file might look like this: | |
54 | ||
55 | | global frame | call frame | call frame | spare capacity | | |
56 | ----------------------------------------------------------------------------------------------------- | |
57 | | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | | | | | | <-- index in buffer | |
58 | ----------------------------------------------------------------------------------------------------- | |
59 | | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | | | | | | <-- index relative to base | |
60 | ----------------------------------------------------------------------------------------------------- | |
61 | | <-globals | temps-> | <-vars | temps-> | <-vars | | |
62 | ^ ^ ^ ^ | |
63 | | | | | | |
64 | buffer base (frame 0) frame 1 frame 2 | |
65 | ||
66 | Since all variables, including globals, are accessed by negative offsets | |
67 | from their register frame pointers, to keep old global offsets correct, new | |
68 | globals must appear at the beginning of the register file, shifting base | |
69 | to the right. | |
70 | ||
71 | If we added one global variable to the register file depicted above, it | |
72 | would look like this: | |
73 | ||
74 | | global frame |< > | |
75 | -------------------------------> < | |
76 | | 0 | 1 | 2 | 3 | 4 | 5 |< >snip< > <-- index in buffer | |
77 | -------------------------------> < | |
78 | | -4 | -3 | -2 | -1 | 0 | 1 |< > <-- index relative to base | |
79 | -------------------------------> < | |
80 | | <-globals | temps-> | | |
81 | ^ ^ | |
82 | | | | |
83 | buffer base (frame 0) | |
84 | ||
85 | As you can see, global offsets relative to base have stayed constant, | |
86 | but base itself has moved. To keep up with possible changes to base, | |
87 | clients keep an indirect pointer, so their calculations update | |
88 | automatically when base changes. | |
89 | ||
90 | For client simplicity, the RegisterFile measures size and capacity from | |
91 | "base", not "buffer". | |
92 | */ | |
93 | ||
94 | class JSGlobalObject; | |
95 | ||
96 | class RegisterFile : public Noncopyable { | |
97 | friend class JIT; | |
98 | public: | |
99 | enum CallFrameHeaderEntry { | |
100 | CallFrameHeaderSize = 8, | |
101 | ||
102 | CodeBlock = -8, | |
103 | ScopeChain = -7, | |
104 | CallerFrame = -6, | |
105 | ReturnPC = -5, // This is either an Instruction* or a pointer into JIT generated code stored as an Instruction*. | |
106 | ReturnValueRegister = -4, | |
107 | ArgumentCount = -3, | |
108 | Callee = -2, | |
109 | OptionalCalleeArguments = -1, | |
110 | }; | |
111 | ||
112 | enum { ProgramCodeThisRegister = -CallFrameHeaderSize - 1 }; | |
113 | enum { ArgumentsRegister = 0 }; | |
114 | ||
115 | static const size_t defaultCapacity = 524288; | |
116 | static const size_t defaultMaxGlobals = 8192; | |
117 | static const size_t commitSize = 1 << 14; | |
118 | // Allow 8k of excess registers before we start trying to reap the registerfile | |
119 | static const ptrdiff_t maxExcessCapacity = 8 * 1024; | |
120 | ||
121 | RegisterFile(size_t capacity = defaultCapacity, size_t maxGlobals = defaultMaxGlobals); | |
122 | ~RegisterFile(); | |
123 | ||
124 | Register* start() const { return m_start; } | |
125 | Register* end() const { return m_end; } | |
126 | size_t size() const { return m_end - m_start; } | |
127 | ||
128 | void setGlobalObject(JSGlobalObject*); | |
129 | bool clearGlobalObject(JSGlobalObject*); | |
130 | JSGlobalObject* globalObject(); | |
131 | ||
132 | bool grow(Register* newEnd); | |
133 | void shrink(Register* newEnd); | |
134 | ||
135 | void setNumGlobals(size_t numGlobals) { m_numGlobals = numGlobals; } | |
136 | int numGlobals() const { return m_numGlobals; } | |
137 | size_t maxGlobals() const { return m_maxGlobals; } | |
138 | ||
139 | Register* lastGlobal() const { return m_start - m_numGlobals; } | |
140 | ||
141 | void markGlobals(MarkStack& markStack, Heap* heap) { heap->markConservatively(markStack, lastGlobal(), m_start); } | |
142 | void markCallFrames(MarkStack& markStack, Heap* heap) { heap->markConservatively(markStack, m_start, m_end); } | |
143 | ||
144 | private: | |
145 | void releaseExcessCapacity(); | |
146 | size_t m_numGlobals; | |
147 | const size_t m_maxGlobals; | |
148 | Register* m_start; | |
149 | Register* m_end; | |
150 | Register* m_max; | |
151 | Register* m_buffer; | |
152 | Register* m_maxUsed; | |
153 | ||
154 | #if HAVE(VIRTUALALLOC) | |
155 | Register* m_commitEnd; | |
156 | #endif | |
157 | ||
158 | WeakGCPtr<JSGlobalObject> m_globalObject; // The global object whose vars are currently stored in the register file. | |
159 | }; | |
160 | ||
161 | // FIXME: Add a generic getpagesize() to WTF, then move this function to WTF as well. | |
162 | inline bool isPageAligned(size_t size) { return size != 0 && size % (8 * 1024) == 0; } | |
163 | ||
164 | inline RegisterFile::RegisterFile(size_t capacity, size_t maxGlobals) | |
165 | : m_numGlobals(0) | |
166 | , m_maxGlobals(maxGlobals) | |
167 | , m_start(0) | |
168 | , m_end(0) | |
169 | , m_max(0) | |
170 | , m_buffer(0) | |
171 | { | |
172 | // Verify that our values will play nice with mmap and VirtualAlloc. | |
173 | ASSERT(isPageAligned(maxGlobals)); | |
174 | ASSERT(isPageAligned(capacity)); | |
175 | ||
176 | size_t bufferLength = (capacity + maxGlobals) * sizeof(Register); | |
177 | #if HAVE(MMAP) | |
178 | m_buffer = static_cast<Register*>(mmap(0, bufferLength, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, VM_TAG_FOR_REGISTERFILE_MEMORY, 0)); | |
179 | if (m_buffer == MAP_FAILED) { | |
180 | #if OS(WINCE) | |
181 | fprintf(stderr, "Could not allocate register file: %d\n", GetLastError()); | |
182 | #else | |
183 | fprintf(stderr, "Could not allocate register file: %d\n", errno); | |
184 | #endif | |
185 | CRASH(); | |
186 | } | |
187 | #elif HAVE(VIRTUALALLOC) | |
188 | m_buffer = static_cast<Register*>(VirtualAlloc(0, roundUpAllocationSize(bufferLength, commitSize), MEM_RESERVE, PAGE_READWRITE)); | |
189 | if (!m_buffer) { | |
190 | #if OS(WINCE) | |
191 | fprintf(stderr, "Could not allocate register file: %d\n", GetLastError()); | |
192 | #else | |
193 | fprintf(stderr, "Could not allocate register file: %d\n", errno); | |
194 | #endif | |
195 | CRASH(); | |
196 | } | |
197 | size_t committedSize = roundUpAllocationSize(maxGlobals * sizeof(Register), commitSize); | |
198 | void* commitCheck = VirtualAlloc(m_buffer, committedSize, MEM_COMMIT, PAGE_READWRITE); | |
199 | if (commitCheck != m_buffer) { | |
200 | #if OS(WINCE) | |
201 | fprintf(stderr, "Could not allocate register file: %d\n", GetLastError()); | |
202 | #else | |
203 | fprintf(stderr, "Could not allocate register file: %d\n", errno); | |
204 | #endif | |
205 | CRASH(); | |
206 | } | |
207 | m_commitEnd = reinterpret_cast<Register*>(reinterpret_cast<char*>(m_buffer) + committedSize); | |
208 | #else | |
209 | /* | |
210 | * If neither MMAP nor VIRTUALALLOC are available - use fastMalloc instead. | |
211 | * | |
212 | * Please note that this is the fallback case, which is non-optimal. | |
213 | * If any possible, the platform should provide for a better memory | |
214 | * allocation mechanism that allows for "lazy commit" or dynamic | |
215 | * pre-allocation, similar to mmap or VirtualAlloc, to avoid waste of memory. | |
216 | */ | |
217 | m_buffer = static_cast<Register*>(fastMalloc(bufferLength)); | |
218 | #endif | |
219 | m_start = m_buffer + maxGlobals; | |
220 | m_end = m_start; | |
221 | m_maxUsed = m_end; | |
222 | m_max = m_start + capacity; | |
223 | } | |
224 | ||
225 | inline void RegisterFile::shrink(Register* newEnd) | |
226 | { | |
227 | if (newEnd >= m_end) | |
228 | return; | |
229 | m_end = newEnd; | |
230 | if (m_end == m_start && (m_maxUsed - m_start) > maxExcessCapacity) | |
231 | releaseExcessCapacity(); | |
232 | } | |
233 | ||
234 | inline bool RegisterFile::grow(Register* newEnd) | |
235 | { | |
236 | if (newEnd < m_end) | |
237 | return true; | |
238 | ||
239 | if (newEnd > m_max) | |
240 | return false; | |
241 | ||
242 | #if !HAVE(MMAP) && HAVE(VIRTUALALLOC) | |
243 | if (newEnd > m_commitEnd) { | |
244 | size_t size = roundUpAllocationSize(reinterpret_cast<char*>(newEnd) - reinterpret_cast<char*>(m_commitEnd), commitSize); | |
245 | if (!VirtualAlloc(m_commitEnd, size, MEM_COMMIT, PAGE_READWRITE)) { | |
246 | #if OS(WINCE) | |
247 | fprintf(stderr, "Could not allocate register file: %d\n", GetLastError()); | |
248 | #else | |
249 | fprintf(stderr, "Could not allocate register file: %d\n", errno); | |
250 | #endif | |
251 | CRASH(); | |
252 | } | |
253 | m_commitEnd = reinterpret_cast<Register*>(reinterpret_cast<char*>(m_commitEnd) + size); | |
254 | } | |
255 | #endif | |
256 | ||
257 | if (newEnd > m_maxUsed) | |
258 | m_maxUsed = newEnd; | |
259 | ||
260 | m_end = newEnd; | |
261 | return true; | |
262 | } | |
263 | ||
264 | } // namespace JSC | |
265 | ||
266 | #endif // RegisterFile_h |