source: webkit/trunk/JavaScriptCore/interpreter/RegisterFile.h@ 39351

Last change on this file since 39351 was 39351, checked in by [email protected], 16 years ago

2008-12-16 Sam Weinig <[email protected]>

Reviewed by Gavin Barraclough.

Fix for https://bugs.webkit.org/show_bug.cgi?id=22837
Remove dependency on the bytecode Instruction buffer in Interpreter::cti_op_call_NotJSFunction
Part of <rdar://problem/6428342>

  • interpreter/CallFrame.h: Added comment regarding returnPC storing a void*.
  • interpreter/Interpreter.cpp: (JSC::bytecodeOffsetForPC): We no longer have any cases of the PC being in the instruction stream for JIT, so we can remove the check. (JSC::Interpreter::cti_op_call_NotJSFunction): Use the CTI_RETURN_ADDRESS as the call frame returnPC as it is only necessary for looking up when throwing an exception.
  • interpreter/RegisterFile.h: (JSC::RegisterFile::): Added comment regarding returnPC storing a void*.
  • jit/JIT.h: Remove ARG_instr4.
  • jit/JITCall.cpp: (JSC::JIT::compileOpCallSetupArgs): Don't pass the instruction pointer.
File size: 7.9 KB
Line 
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
115 RegisterFile(size_t capacity = defaultCapacity, size_t maxGlobals = defaultMaxGlobals)
116 : m_numGlobals(0)
117 , m_maxGlobals(maxGlobals)
118 , m_start(0)
119 , m_end(0)
120 , m_max(0)
121 , m_buffer(0)
122 , m_globalObject(0)
123 {
124 size_t bufferLength = (capacity + maxGlobals) * sizeof(Register);
125#if HAVE(MMAP)
126 m_buffer = static_cast<Register*>(mmap(0, bufferLength, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0));
127 if (m_buffer == MAP_FAILED) {
128 fprintf(stderr, "Could not allocate register file: %d\n", errno);
129 CRASH();
130 }
131#elif HAVE(VIRTUALALLOC)
132 // FIXME: Use VirtualAlloc, and commit pages as we go.
133 m_buffer = static_cast<Register*>(fastMalloc(bufferLength));
134#else
135 #error "Don't know how to reserve virtual memory on this platform."
136#endif
137 m_start = m_buffer + maxGlobals;
138 m_end = m_start;
139 m_max = m_start + capacity;
140 }
141
142 ~RegisterFile();
143
144 Register* start() const { return m_start; }
145 Register* end() const { return m_end; }
146 size_t size() const { return m_end - m_start; }
147
148 void setGlobalObject(JSGlobalObject* globalObject) { m_globalObject = globalObject; }
149 JSGlobalObject* globalObject() { return m_globalObject; }
150
151 void shrink(Register* newEnd)
152 {
153 if (newEnd < m_end)
154 m_end = newEnd;
155 }
156
157 bool grow(Register* newEnd)
158 {
159 if (newEnd > m_end) {
160 if (newEnd > m_max)
161 return false;
162#if !HAVE(MMAP) && HAVE(VIRTUALALLOC)
163 // FIXME: Use VirtualAlloc, and commit pages as we go.
164#endif
165 m_end = newEnd;
166 }
167 return true;
168 }
169
170 void setNumGlobals(size_t numGlobals) { m_numGlobals = numGlobals; }
171 int numGlobals() const { return m_numGlobals; }
172 size_t maxGlobals() const { return m_maxGlobals; }
173
174 Register* lastGlobal() const { return m_start - m_numGlobals; }
175
176 void markGlobals(Heap* heap) { heap->markConservatively(lastGlobal(), m_start); }
177 void markCallFrames(Heap* heap) { heap->markConservatively(m_start, m_end); }
178
179 private:
180 size_t m_numGlobals;
181 const size_t m_maxGlobals;
182 Register* m_start;
183 Register* m_end;
184 Register* m_max;
185 Register* m_buffer;
186 JSGlobalObject* m_globalObject; // The global object whose vars are currently stored in the register file.
187 };
188
189} // namespace JSC
190
191#endif // RegisterFile_h
Note: See TracBrowser for help on using the repository browser.