source: webkit/trunk/JavaScriptCore/bytecode/CodeBlock.h@ 39072

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

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

Reviewed by Cameron Zwarich,

Move CodeBlock constructor into the .cpp file.

Sunspider reports a .7% progression, but I can only assume this
is noise.

  • bytecode/CodeBlock.cpp: (JSC::CodeBlock::CodeBlock):
  • bytecode/CodeBlock.h:
File size: 16.9 KB
Line 
1/*
2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Cameron Zwarich <[email protected]>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef CodeBlock_h
31#define CodeBlock_h
32
33#include "EvalCodeCache.h"
34#include "Instruction.h"
35#include "JSGlobalObject.h"
36#include "JumpTable.h"
37#include "Nodes.h"
38#include "RegExp.h"
39#include "UString.h"
40#include <wtf/RefPtr.h>
41#include <wtf/Vector.h>
42
43namespace JSC {
44
45 class ExecState;
46
47 enum CodeType { GlobalCode, EvalCode, FunctionCode };
48
49 static ALWAYS_INLINE int missingThisObjectMarker() { return std::numeric_limits<int>::max(); }
50
51 struct HandlerInfo {
52 uint32_t start;
53 uint32_t end;
54 uint32_t target;
55 uint32_t scopeDepth;
56 void* nativeCode;
57 };
58
59 struct ExpressionRangeInfo {
60 enum {
61 MaxOffset = (1 << 7) - 1,
62 MaxDivot = (1 << 25) - 1
63 };
64 uint32_t instructionOffset : 25;
65 uint32_t divotPoint : 25;
66 uint32_t startOffset : 7;
67 uint32_t endOffset : 7;
68 };
69
70 struct LineInfo {
71 uint32_t instructionOffset;
72 int32_t lineNumber;
73 };
74
75 struct StructureStubInfo {
76 StructureStubInfo(unsigned bytecodeIndex)
77 : bytecodeIndex(bytecodeIndex)
78 , stubRoutine(0)
79 , callReturnLocation(0)
80 , hotPathBegin(0)
81 {
82 }
83
84 unsigned bytecodeIndex;
85 void* stubRoutine;
86 void* callReturnLocation;
87 void* hotPathBegin;
88 };
89
90 struct CallLinkInfo {
91 CallLinkInfo()
92 : callReturnLocation(0)
93 , hotPathBegin(0)
94 , hotPathOther(0)
95 , coldPathOther(0)
96 , callee(0)
97 {
98 }
99
100 unsigned bytecodeIndex;
101 void* callReturnLocation;
102 void* hotPathBegin;
103 void* hotPathOther;
104 void* coldPathOther;
105 CodeBlock* callee;
106 unsigned position;
107
108 void setUnlinked() { callee = 0; }
109 bool isLinked() { return callee; }
110 };
111
112 inline void* getStructureStubInfoReturnLocation(StructureStubInfo* structureStubInfo)
113 {
114 return structureStubInfo->callReturnLocation;
115 }
116
117 inline void* getCallLinkInfoReturnLocation(CallLinkInfo* callLinkInfo)
118 {
119 return callLinkInfo->callReturnLocation;
120 }
121
122 // Binary chop algorithm, calls valueAtPosition on pre-sorted elements in array,
123 // compares result with key (KeyTypes should be comparable with '--', '<', '>').
124 // Optimized for cases where the array contains the key, checked by assertions.
125 template<typename ArrayType, typename KeyType, KeyType(*valueAtPosition)(ArrayType*)>
126 inline ArrayType* binaryChop(ArrayType* array, size_t size, KeyType key)
127 {
128 // The array must contain at least one element (pre-condition, array does conatin key).
129 // If the array only contains one element, no need to do the comparison.
130 while (size > 1) {
131 // Pick an element to check, half way through the array, and read the value.
132 int pos = (size - 1) >> 1;
133 KeyType val = valueAtPosition(&array[pos]);
134
135 // If the key matches, success!
136 if (val == key)
137 return &array[pos];
138 // The item we are looking for is smaller than the item being check; reduce the value of 'size',
139 // chopping off the right hand half of the array.
140 else if (key < val)
141 size = pos;
142 // Discard all values in the left hand half of the array, up to and including the item at pos.
143 else {
144 size -= (pos + 1);
145 array += (pos + 1);
146 }
147
148 // 'size' should never reach zero.
149 ASSERT(size);
150 }
151
152 // If we reach this point we've chopped down to one element, no need to check it matches
153 ASSERT(size == 1);
154 ASSERT(key == valueAtPosition(&array[0]));
155 return &array[0];
156 }
157
158 class CodeBlock {
159 friend class JIT;
160 public:
161 CodeBlock(ScopeNode* ownerNode, CodeType, PassRefPtr<SourceProvider>, unsigned sourceOffset);
162 ~CodeBlock();
163
164#if ENABLE(JIT)
165 void unlinkCallers();
166#endif
167
168 void addCaller(CallLinkInfo* caller)
169 {
170 caller->callee = this;
171 caller->position = m_linkedCallerList.size();
172 m_linkedCallerList.append(caller);
173 }
174
175 void removeCaller(CallLinkInfo* caller)
176 {
177 unsigned pos = caller->position;
178 unsigned lastPos = m_linkedCallerList.size() - 1;
179
180 if (pos != lastPos) {
181 m_linkedCallerList[pos] = m_linkedCallerList[lastPos];
182 m_linkedCallerList[pos]->position = pos;
183 }
184 m_linkedCallerList.shrink(lastPos);
185 }
186
187 inline bool isKnownNotImmediate(int index)
188 {
189 if (index == m_thisRegister)
190 return true;
191
192 if (isConstantRegisterIndex(index))
193 return !JSImmediate::isImmediate(getConstant(index));
194
195 return false;
196 }
197
198 ALWAYS_INLINE bool isConstantRegisterIndex(int index)
199 {
200 return index >= m_numVars && index < m_numVars + m_numConstants;
201 }
202
203 ALWAYS_INLINE JSValue* getConstant(int index)
204 {
205 return m_constantRegisters[index - m_numVars].getJSValue();
206 }
207
208 ALWAYS_INLINE bool isTemporaryRegisterIndex(int index)
209 {
210 return index >= m_numVars + m_numConstants;
211 }
212
213#if !defined(NDEBUG) || ENABLE_OPCODE_SAMPLING
214 void dump(ExecState*) const;
215 void printStructures(const Instruction*) const;
216 void printStructure(const char* name, const Instruction*, int operand) const;
217#endif
218 int expressionRangeForVPC(const Instruction*, int& divot, int& startOffset, int& endOffset);
219 int lineNumberForVPC(const Instruction* vPC);
220 bool getHandlerForVPC(const Instruction* vPC, Instruction*& target, int& scopeDepth);
221 void* nativeExceptionCodeForHandlerVPC(const Instruction* handlerVPC);
222
223 void mark();
224 void refStructures(Instruction* vPC) const;
225 void derefStructures(Instruction* vPC) const;
226
227 StructureStubInfo& getStubInfo(void* returnAddress)
228 {
229 return *(binaryChop<StructureStubInfo, void*, getStructureStubInfoReturnLocation>(m_propertyAccessInstructions.begin(), m_propertyAccessInstructions.size(), returnAddress));
230 }
231
232 CallLinkInfo& getCallLinkInfo(void* returnAddress)
233 {
234 return *(binaryChop<CallLinkInfo, void*, getCallLinkInfoReturnLocation>(m_callLinkInfos.begin(), m_callLinkInfos.size(), returnAddress));
235 }
236
237
238 Vector<Instruction>& instructions() { return m_instructions; }
239#if ENABLE(JIT)
240 void setJITCode(void* jitCode) { m_jitCode = jitCode; }
241 void* jitCode() { return m_jitCode; }
242#endif
243
244 ScopeNode* ownerNode() const { return m_ownerNode; }
245
246 void setGlobalData(JSGlobalData* globalData) { m_globalData = globalData; }
247
248 void setThisRegister(int thisRegister) { m_thisRegister = thisRegister; }
249 int thisRegister() const { return m_thisRegister; }
250
251 void setNeedsFullScopeChain(bool needsFullScopeChain) { m_needsFullScopeChain = needsFullScopeChain; }
252 bool needsFullScopeChain() const { return m_needsFullScopeChain; }
253 void setUsesEval(bool usesEval) { m_usesEval = usesEval; }
254 bool usesEval() const { return m_usesEval; }
255 void setUsesArguments(bool usesArguments) { m_usesArguments = usesArguments; }
256 bool usesArguments() const { return m_usesArguments; }
257
258 CodeType codeType() const { return m_codeType; }
259
260 SourceProvider* source() const { return m_source.get(); }
261 unsigned sourceOffset() const { return m_sourceOffset; }
262
263 void addGlobalResolveInstruction(unsigned globalResolveInstructions) { m_globalResolveInstructions.append(globalResolveInstructions); }
264
265 size_t numberOfPropertyAccessInstructions() const { return m_propertyAccessInstructions.size(); }
266 void addPropertyAccessInstruction(unsigned propertyAccessInstructions) { m_propertyAccessInstructions.append(StructureStubInfo(propertyAccessInstructions)); }
267 StructureStubInfo& propertyAccessInstruction(int index) { return m_propertyAccessInstructions[index]; }
268
269 size_t numberOfCallLinkInfos() const { return m_callLinkInfos.size(); }
270 void addCallLinkInfo() { m_callLinkInfos.append(CallLinkInfo()); }
271 CallLinkInfo& callLinkInfo(int index) { return m_callLinkInfos[index]; }
272
273 size_t numberOfJumpTargets() const { return m_jumpTargets.size(); }
274 void addJumpTarget(unsigned jumpTarget) { m_jumpTargets.append(jumpTarget); }
275 unsigned jumpTarget(int index) const { return m_jumpTargets[index]; }
276 unsigned lastJumpTarget() const { return m_jumpTargets.last(); }
277
278 size_t numberOfExceptionHandlers() const { return m_exceptionHandlers.size(); }
279 void addExceptionHandler(const HandlerInfo& hanler) { return m_exceptionHandlers.append(hanler); }
280 HandlerInfo& exceptionHandler(int index) { return m_exceptionHandlers[index]; }
281
282 void addExpressionInfo(const ExpressionRangeInfo& expressionInfo) { return m_expressionInfo.append(expressionInfo); }
283
284 size_t numberOfLineInfos() const { return m_lineInfo.size(); }
285 void addLineInfo(const LineInfo& lineInfo) { return m_lineInfo.append(lineInfo); }
286 LineInfo& lastLineInfo() { return m_lineInfo.last(); }
287
288#if ENABLE(JIT)
289 HashMap<void*, unsigned>& jitReturnAddressVPCMap() { return m_jitReturnAddressVPCMap; }
290#endif
291
292 // Constant Pool
293
294 size_t numberOfIdentifiers() const { return m_identifiers.size(); }
295 void addIdentifier(const Identifier& i) { return m_identifiers.append(i); }
296 Identifier& identifier(int index) { return m_identifiers[index]; }
297
298 size_t numberOfConstantRegisters() const { return m_constantRegisters.size(); }
299 void addConstantRegister(const Register& r) { return m_constantRegisters.append(r); }
300 Register& constantRegister(int index) { return m_constantRegisters[index]; }
301
302 unsigned addFunction(FuncDeclNode* n) { unsigned size = m_functions.size(); m_functions.append(n); return size; }
303 FuncDeclNode* function(int index) const { return m_functions[index].get(); }
304
305 unsigned addFunctionExpression(FuncExprNode* n) { unsigned size = m_functionExpressions.size(); m_functionExpressions.append(n); return size; }
306 FuncExprNode* functionExpression(int index) const { return m_functionExpressions[index].get(); }
307
308 unsigned addUnexpectedConstant(JSValue* v) { unsigned size = m_unexpectedConstants.size(); m_unexpectedConstants.append(v); return size; }
309 JSValue* unexpectedConstant(int index) const { return m_unexpectedConstants[index]; }
310
311 unsigned addRegExp(RegExp* r) { unsigned size = m_regexps.size(); m_regexps.append(r); return size; }
312 RegExp* regexp(int index) const { return m_regexps[index].get(); }
313
314 // Jump Tables
315
316 size_t numberOfImmediateSwitchJumpTables() const { return m_immediateSwitchJumpTables.size(); }
317 SimpleJumpTable& addImmediateSwitchJumpTable() { m_immediateSwitchJumpTables.append(SimpleJumpTable()); return m_immediateSwitchJumpTables.last(); }
318 SimpleJumpTable& immediateSwitchJumpTable(int tableIndex) { return m_immediateSwitchJumpTables[tableIndex]; }
319
320 size_t numberOfCharacterSwitchJumpTables() const { return m_characterSwitchJumpTables.size(); }
321 SimpleJumpTable& addCharacterSwitchJumpTable() { m_characterSwitchJumpTables.append(SimpleJumpTable()); return m_characterSwitchJumpTables.last(); }
322 SimpleJumpTable& characterSwitchJumpTable(int tableIndex) { return m_characterSwitchJumpTables[tableIndex]; }
323
324 size_t numberOfStringSwitchJumpTables() const { return m_stringSwitchJumpTables.size(); }
325 StringJumpTable& addStringSwitchJumpTable() { m_stringSwitchJumpTables.append(StringJumpTable()); return m_stringSwitchJumpTables.last(); }
326 StringJumpTable& stringSwitchJumpTable(int tableIndex) { return m_stringSwitchJumpTables[tableIndex]; }
327
328
329 SymbolTable& symbolTable() { return m_symbolTable; }
330 EvalCodeCache& evalCodeCache() { return m_evalCodeCache; }
331
332 void shrinkToFit();
333
334 // FIXME: Make these remaining members private.
335
336 int m_numCalleeRegisters;
337 // NOTE: numConstants holds the number of constant registers allocated
338 // by the code generator, not the number of constant registers used.
339 // (Duplicate constants are uniqued during code generation, and spare
340 // constant registers may be allocated.)
341 int m_numConstants;
342 int m_numVars;
343 int m_numParameters;
344
345 private:
346#if !defined(NDEBUG) || ENABLE(OPCODE_SAMPLING)
347 void dump(ExecState*, const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator&) const;
348#endif
349
350 ScopeNode* m_ownerNode;
351 JSGlobalData* m_globalData;
352
353 Vector<Instruction> m_instructions;
354#if ENABLE(JIT)
355 void* m_jitCode;
356#endif
357
358 int m_thisRegister;
359
360 bool m_needsFullScopeChain;
361 bool m_usesEval;
362 bool m_usesArguments;
363
364 CodeType m_codeType;
365
366 RefPtr<SourceProvider> m_source;
367 unsigned m_sourceOffset;
368
369 Vector<unsigned> m_globalResolveInstructions;
370 Vector<StructureStubInfo> m_propertyAccessInstructions;
371 Vector<CallLinkInfo> m_callLinkInfos;
372 Vector<CallLinkInfo*> m_linkedCallerList;
373
374 Vector<unsigned> m_jumpTargets;
375
376 Vector<HandlerInfo> m_exceptionHandlers;
377 Vector<ExpressionRangeInfo> m_expressionInfo;
378 Vector<LineInfo> m_lineInfo;
379
380#if ENABLE(JIT)
381 HashMap<void*, unsigned> m_jitReturnAddressVPCMap;
382#endif
383
384 // Constant Pool
385 Vector<Identifier> m_identifiers;
386 Vector<Register> m_constantRegisters;
387 Vector<RefPtr<FuncDeclNode> > m_functions;
388 Vector<RefPtr<FuncExprNode> > m_functionExpressions;
389 Vector<JSValue*> m_unexpectedConstants;
390 Vector<RefPtr<RegExp> > m_regexps;
391
392 // Jump Tables
393 Vector<SimpleJumpTable> m_immediateSwitchJumpTables;
394 Vector<SimpleJumpTable> m_characterSwitchJumpTables;
395 Vector<StringJumpTable> m_stringSwitchJumpTables;
396
397 SymbolTable m_symbolTable;
398
399 EvalCodeCache m_evalCodeCache;
400 };
401
402 // Program code is not marked by any function, so we make the global object
403 // responsible for marking it.
404
405 class ProgramCodeBlock : public CodeBlock {
406 public:
407 ProgramCodeBlock(ScopeNode* ownerNode, CodeType codeType, JSGlobalObject* globalObject, PassRefPtr<SourceProvider> sourceProvider)
408 : CodeBlock(ownerNode, codeType, sourceProvider, 0)
409 , m_globalObject(globalObject)
410 {
411 m_globalObject->codeBlocks().add(this);
412 }
413
414 ~ProgramCodeBlock()
415 {
416 if (m_globalObject)
417 m_globalObject->codeBlocks().remove(this);
418 }
419
420 void clearGlobalObject() { m_globalObject = 0; }
421
422 private:
423 JSGlobalObject* m_globalObject; // For program and eval nodes, the global object that marks the constant pool.
424 };
425
426 class EvalCodeBlock : public ProgramCodeBlock {
427 public:
428 EvalCodeBlock(ScopeNode* ownerNode, JSGlobalObject* globalObject, PassRefPtr<SourceProvider> sourceProvider)
429 : ProgramCodeBlock(ownerNode, EvalCode, globalObject, sourceProvider)
430 {
431 }
432 };
433
434} // namespace JSC
435
436#endif // CodeBlock_h
Note: See TracBrowser for help on using the repository browser.