]> git.saurik.com Git - apple/javascriptcore.git/blame - interpreter/RegisterFile.h
JavaScriptCore-525.tar.gz
[apple/javascriptcore.git] / interpreter / RegisterFile.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 *
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 "Register.h"
33#include "Collector.h"
34#include <wtf/Noncopyable.h>
35
36#if HAVE(MMAP)
37#include <errno.h>
38#include <stdio.h>
39#include <sys/mman.h>
40#endif
41
42namespace JSC {
43
44/*
45 A register file is a stack of register frames. We represent a register
46 frame by its offset from "base", the logical first entry in the register
47 file. The bottom-most register frame's offset from base is 0.
48
49 In a program where function "a" calls function "b" (global code -> a -> b),
50 the register file might look like this:
51
52 | global frame | call frame | call frame | spare capacity |
53 -----------------------------------------------------------------------------------------------------
54 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | | | | | | <-- index in buffer
55 -----------------------------------------------------------------------------------------------------
56 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | | | | | | <-- index relative to base
57 -----------------------------------------------------------------------------------------------------
58 | <-globals | temps-> | <-vars | temps-> | <-vars |
59 ^ ^ ^ ^
60 | | | |
61 buffer base (frame 0) frame 1 frame 2
62
63 Since all variables, including globals, are accessed by negative offsets
64 from their register frame pointers, to keep old global offsets correct, new
65 globals must appear at the beginning of the register file, shifting base
66 to the right.
67
68 If we added one global variable to the register file depicted above, it
69 would look like this:
70
71 | global frame |< >
72 -------------------------------> <
73 | 0 | 1 | 2 | 3 | 4 | 5 |< >snip< > <-- index in buffer
74 -------------------------------> <
75 | -4 | -3 | -2 | -1 | 0 | 1 |< > <-- index relative to base
76 -------------------------------> <
77 | <-globals | temps-> |
78 ^ ^
79 | |
80 buffer base (frame 0)
81
82 As you can see, global offsets relative to base have stayed constant,
83 but base itself has moved. To keep up with possible changes to base,
84 clients keep an indirect pointer, so their calculations update
85 automatically when base changes.
86
87 For client simplicity, the RegisterFile measures size and capacity from
88 "base", not "buffer".
89*/
90
91 class JSGlobalObject;
92
93 class RegisterFile : Noncopyable {
94 friend class JIT;
95 public:
96 enum CallFrameHeaderEntry {
97 CallFrameHeaderSize = 8,
98
99 CodeBlock = -8,
100 ScopeChain = -7,
101 CallerFrame = -6,
102 ReturnPC = -5, // This is either an Instruction* or a pointer into JIT generated code stored as an Instruction*.
103 ReturnValueRegister = -4,
104 ArgumentCount = -3,
105 Callee = -2,
106 OptionalCalleeArguments = -1,
107 };
108
109 enum { ProgramCodeThisRegister = -CallFrameHeaderSize - 1 };
110 enum { ArgumentsRegister = 0 };
111
112 static const size_t defaultCapacity = 524288;
113 static const size_t defaultMaxGlobals = 8192;
114 static const size_t allocationSize = 1 << 14;
115 static const size_t allocationSizeMask = allocationSize - 1;
116
117 RegisterFile(size_t capacity = defaultCapacity, size_t maxGlobals = defaultMaxGlobals)
118 : m_numGlobals(0)
119 , m_maxGlobals(maxGlobals)
120 , m_start(0)
121 , m_end(0)
122 , m_max(0)
123 , m_buffer(0)
124 , m_globalObject(0)
125 {
126 size_t bufferLength = (capacity + maxGlobals) * sizeof(Register);
127#if HAVE(MMAP)
128 m_buffer = static_cast<Register*>(mmap(0, bufferLength, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0));
129 if (m_buffer == MAP_FAILED) {
130 fprintf(stderr, "Could not allocate register file: %d\n", errno);
131 CRASH();
132 }
133#elif HAVE(VIRTUALALLOC)
134 // Ensure bufferLength is a multiple of allocation size
135 bufferLength = (bufferLength + allocationSizeMask) & ~allocationSizeMask;
136 m_buffer = static_cast<Register*>(VirtualAlloc(0, bufferLength, MEM_RESERVE, PAGE_READWRITE));
137 if (!m_buffer) {
138 fprintf(stderr, "Could not allocate register file: %d\n", errno);
139 CRASH();
140 }
141 int initialAllocation = (maxGlobals * sizeof(Register) + allocationSizeMask) & ~allocationSizeMask;
142 void* commitCheck = VirtualAlloc(m_buffer, initialAllocation, MEM_COMMIT, PAGE_READWRITE);
143 if (commitCheck != m_buffer) {
144 fprintf(stderr, "Could not allocate register file: %d\n", errno);
145 CRASH();
146 }
147 m_maxCommitted = reinterpret_cast<Register*>(reinterpret_cast<char*>(m_buffer) + initialAllocation);
148#else
149 #error "Don't know how to reserve virtual memory on this platform."
150#endif
151 m_start = m_buffer + maxGlobals;
152 m_end = m_start;
153 m_max = m_start + capacity;
154 }
155
156 ~RegisterFile();
157
158 Register* start() const { return m_start; }
159 Register* end() const { return m_end; }
160 size_t size() const { return m_end - m_start; }
161
162 void setGlobalObject(JSGlobalObject* globalObject) { m_globalObject = globalObject; }
163 JSGlobalObject* globalObject() { return m_globalObject; }
164
165 void shrink(Register* newEnd)
166 {
167 if (newEnd < m_end)
168 m_end = newEnd;
169 }
170
171 bool grow(Register* newEnd)
172 {
173 if (newEnd > m_end) {
174 if (newEnd > m_max)
175 return false;
176#if !HAVE(MMAP) && HAVE(VIRTUALALLOC)
177 if (newEnd > m_maxCommitted) {
178 ptrdiff_t additionalAllocation = ((reinterpret_cast<char*>(newEnd) - reinterpret_cast<char*>(m_maxCommitted)) + allocationSizeMask) & ~allocationSizeMask;
179 if (!VirtualAlloc(m_maxCommitted, additionalAllocation, MEM_COMMIT, PAGE_READWRITE)) {
180 fprintf(stderr, "Could not allocate register file: %d\n", errno);
181 CRASH();
182 }
183 m_maxCommitted = reinterpret_cast<Register*>(reinterpret_cast<char*>(m_maxCommitted) + additionalAllocation);
184 }
185#endif
186 m_end = newEnd;
187 }
188 return true;
189 }
190
191 void setNumGlobals(size_t numGlobals) { m_numGlobals = numGlobals; }
192 int numGlobals() const { return m_numGlobals; }
193 size_t maxGlobals() const { return m_maxGlobals; }
194
195 Register* lastGlobal() const { return m_start - m_numGlobals; }
196
197 void markGlobals(Heap* heap) { heap->markConservatively(lastGlobal(), m_start); }
198 void markCallFrames(Heap* heap) { heap->markConservatively(m_start, m_end); }
199
200 private:
201 size_t m_numGlobals;
202 const size_t m_maxGlobals;
203 Register* m_start;
204 Register* m_end;
205 Register* m_max;
206 Register* m_buffer;
207#if HAVE(VIRTUALALLOC)
208 Register* m_maxCommitted;
209#endif
210
211 JSGlobalObject* m_globalObject; // The global object whose vars are currently stored in the register file.
212 };
213
214} // namespace JSC
215
216#endif // RegisterFile_h