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

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

2008-12-13 Gavin Barraclough <[email protected]>

Build fix only, no review.

  • bytecode/CodeBlock.h:
File size: 19.7 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
43#if ENABLE(JIT)
44#include "StructureStubInfo.h"
45#endif
46
47namespace JSC {
48
49 class ExecState;
50
51 enum CodeType { GlobalCode, EvalCode, FunctionCode };
52
53 static ALWAYS_INLINE int missingThisObjectMarker() { return std::numeric_limits<int>::max(); }
54
55 struct HandlerInfo {
56 uint32_t start;
57 uint32_t end;
58 uint32_t target;
59 uint32_t scopeDepth;
60#if ENABLE(JIT)
61 void* nativeCode;
62#endif
63 };
64
65#if ENABLE(JIT)
66 // The code, and the associated pool from which it was allocated.
67 struct JITCodeRef {
68 void* code;
69 RefPtr<ExecutablePool> executablePool;
70
71 JITCodeRef()
72 : code(0)
73 {
74 }
75
76 JITCodeRef(void* code, PassRefPtr<ExecutablePool> executablePool)
77 : code(code)
78 , executablePool(executablePool)
79 {
80 }
81 };
82#endif
83
84 struct ExpressionRangeInfo {
85 enum {
86 MaxOffset = (1 << 7) - 1,
87 MaxDivot = (1 << 25) - 1
88 };
89 uint32_t instructionOffset : 25;
90 uint32_t divotPoint : 25;
91 uint32_t startOffset : 7;
92 uint32_t endOffset : 7;
93 };
94
95 struct LineInfo {
96 uint32_t instructionOffset;
97 int32_t lineNumber;
98 };
99
100 // Both op_construct and op_instanceof require a use of op_get_by_id to get
101 // the prototype property from an object. The exception messages for exceptions
102 // thrown by these instances op_get_by_id need to reflect this.
103 struct GetByIdExceptionInfo {
104 unsigned bytecodeOffset : 31;
105 bool isOpConstruct : 1;
106 };
107
108#if ENABLE(JIT)
109 struct CallLinkInfo {
110 CallLinkInfo()
111 : callReturnLocation(0)
112 , hotPathBegin(0)
113 , hotPathOther(0)
114 , coldPathOther(0)
115 , callee(0)
116 {
117 }
118
119 unsigned bytecodeIndex;
120 void* callReturnLocation;
121 void* hotPathBegin;
122 void* hotPathOther;
123 void* coldPathOther;
124 CodeBlock* callee;
125 unsigned position;
126
127 void setUnlinked() { callee = 0; }
128 bool isLinked() { return callee; }
129 };
130
131 struct GlobalResolveInfo {
132 GlobalResolveInfo()
133 : structure(0)
134 , offset(0)
135 {
136 }
137
138 Structure* structure;
139 unsigned offset;
140 };
141
142 struct PC {
143 PC(void* nativePC, unsigned bytecodeIndex)
144 : nativePC(nativePC)
145 , bytecodeIndex(bytecodeIndex)
146 {
147 }
148
149 void* nativePC;
150 unsigned bytecodeIndex;
151 };
152
153 // valueAtPosition helpers for the binaryChop algorithm below.
154
155 inline void* getStructureStubInfoReturnLocation(StructureStubInfo* structureStubInfo)
156 {
157 return structureStubInfo->callReturnLocation;
158 }
159
160 inline void* getCallLinkInfoReturnLocation(CallLinkInfo* callLinkInfo)
161 {
162 return callLinkInfo->callReturnLocation;
163 }
164
165 inline void* getNativePC(PC* pc)
166 {
167 return pc->nativePC;
168 }
169
170 // Binary chop algorithm, calls valueAtPosition on pre-sorted elements in array,
171 // compares result with key (KeyTypes should be comparable with '--', '<', '>').
172 // Optimized for cases where the array contains the key, checked by assertions.
173 template<typename ArrayType, typename KeyType, KeyType(*valueAtPosition)(ArrayType*)>
174 inline ArrayType* binaryChop(ArrayType* array, size_t size, KeyType key)
175 {
176 // The array must contain at least one element (pre-condition, array does conatin key).
177 // If the array only contains one element, no need to do the comparison.
178 while (size > 1) {
179 // Pick an element to check, half way through the array, and read the value.
180 int pos = (size - 1) >> 1;
181 KeyType val = valueAtPosition(&array[pos]);
182
183 // If the key matches, success!
184 if (val == key)
185 return &array[pos];
186 // The item we are looking for is smaller than the item being check; reduce the value of 'size',
187 // chopping off the right hand half of the array.
188 else if (key < val)
189 size = pos;
190 // Discard all values in the left hand half of the array, up to and including the item at pos.
191 else {
192 size -= (pos + 1);
193 array += (pos + 1);
194 }
195
196 // 'size' should never reach zero.
197 ASSERT(size);
198 }
199
200 // If we reach this point we've chopped down to one element, no need to check it matches
201 ASSERT(size == 1);
202 ASSERT(key == valueAtPosition(&array[0]));
203 return &array[0];
204 }
205#endif
206
207 class CodeBlock {
208 friend class JIT;
209 public:
210 CodeBlock(ScopeNode* ownerNode, CodeType, PassRefPtr<SourceProvider>, unsigned sourceOffset);
211 ~CodeBlock();
212
213 void mark();
214 void refStructures(Instruction* vPC) const;
215 void derefStructures(Instruction* vPC) const;
216#if ENABLE(JIT)
217 void unlinkCallers();
218#endif
219
220 static void dumpStatistics();
221
222#if !defined(NDEBUG) || ENABLE_OPCODE_SAMPLING
223 void dump(ExecState*) const;
224 void printStructures(const Instruction*) const;
225 void printStructure(const char* name, const Instruction*, int operand) const;
226#endif
227
228 inline bool isKnownNotImmediate(int index)
229 {
230 if (index == m_thisRegister)
231 return true;
232
233 if (isConstantRegisterIndex(index))
234 return !JSImmediate::isImmediate(getConstant(index));
235
236 return false;
237 }
238
239 ALWAYS_INLINE bool isConstantRegisterIndex(int index)
240 {
241 return index >= m_numVars && index < m_numVars + m_numConstants;
242 }
243
244 ALWAYS_INLINE JSValue* getConstant(int index)
245 {
246 return m_constantRegisters[index - m_numVars].getJSValue();
247 }
248
249 ALWAYS_INLINE bool isTemporaryRegisterIndex(int index)
250 {
251 return index >= m_numVars + m_numConstants;
252 }
253
254 HandlerInfo* handlerForBytecodeOffset(unsigned bytecodeOffset);
255 int lineNumberForBytecodeOffset(unsigned bytecodeOffset);
256 int expressionRangeForBytecodeOffset(unsigned bytecodeOffset, int& divot, int& startOffset, int& endOffset);
257 bool getByIdExceptionInfoForBytecodeOffset(unsigned bytecodeOffset, OpcodeID&);
258
259#if ENABLE(JIT)
260 void addCaller(CallLinkInfo* caller)
261 {
262 caller->callee = this;
263 caller->position = m_linkedCallerList.size();
264 m_linkedCallerList.append(caller);
265 }
266
267 void removeCaller(CallLinkInfo* caller)
268 {
269 unsigned pos = caller->position;
270 unsigned lastPos = m_linkedCallerList.size() - 1;
271
272 if (pos != lastPos) {
273 m_linkedCallerList[pos] = m_linkedCallerList[lastPos];
274 m_linkedCallerList[pos]->position = pos;
275 }
276 m_linkedCallerList.shrink(lastPos);
277 }
278
279 StructureStubInfo& getStubInfo(void* returnAddress)
280 {
281 return *(binaryChop<StructureStubInfo, void*, getStructureStubInfoReturnLocation>(m_structureStubInfos.begin(), m_structureStubInfos.size(), returnAddress));
282 }
283
284 CallLinkInfo& getCallLinkInfo(void* returnAddress)
285 {
286 return *(binaryChop<CallLinkInfo, void*, getCallLinkInfoReturnLocation>(m_callLinkInfos.begin(), m_callLinkInfos.size(), returnAddress));
287 }
288
289 unsigned getBytecodeIndex(void* nativePC)
290 {
291 return binaryChop<PC, void*, getNativePC>(m_pcVector.begin(), m_pcVector.size(), nativePC)->bytecodeIndex;
292 }
293#endif
294
295 Vector<Instruction>& instructions() { return m_instructions; }
296
297#if ENABLE(JIT)
298 void setJITCode(JITCodeRef& jitCode) { m_jitCode = jitCode; }
299 void* jitCode() { return m_jitCode.code; }
300 ExecutablePool* executablePool() { return m_jitCode.executablePool.get(); }
301#endif
302
303 ScopeNode* ownerNode() const { return m_ownerNode; }
304
305 void setGlobalData(JSGlobalData* globalData) { m_globalData = globalData; }
306
307 void setThisRegister(int thisRegister) { m_thisRegister = thisRegister; }
308 int thisRegister() const { return m_thisRegister; }
309
310 void setNeedsFullScopeChain(bool needsFullScopeChain) { m_needsFullScopeChain = needsFullScopeChain; }
311 bool needsFullScopeChain() const { return m_needsFullScopeChain; }
312 void setUsesEval(bool usesEval) { m_usesEval = usesEval; }
313 bool usesEval() const { return m_usesEval; }
314 void setUsesArguments(bool usesArguments) { m_usesArguments = usesArguments; }
315 bool usesArguments() const { return m_usesArguments; }
316
317 CodeType codeType() const { return m_codeType; }
318
319 SourceProvider* source() const { return m_source.get(); }
320 unsigned sourceOffset() const { return m_sourceOffset; }
321
322 size_t numberOfJumpTargets() const { return m_jumpTargets.size(); }
323 void addJumpTarget(unsigned jumpTarget) { m_jumpTargets.append(jumpTarget); }
324 unsigned jumpTarget(int index) const { return m_jumpTargets[index]; }
325 unsigned lastJumpTarget() const { return m_jumpTargets.last(); }
326
327 size_t numberOfExceptionHandlers() const { return m_rareData ? m_rareData->m_exceptionHandlers.size() : 0; }
328 void addExceptionHandler(const HandlerInfo& hanler) { createRareDataIfNecessary(); return m_rareData->m_exceptionHandlers.append(hanler); }
329 HandlerInfo& exceptionHandler(int index) { ASSERT(m_rareData); return m_rareData->m_exceptionHandlers[index]; }
330
331 void addExpressionInfo(const ExpressionRangeInfo& expressionInfo) { return m_expressionInfo.append(expressionInfo); }
332 void addGetByIdExceptionInfo(const GetByIdExceptionInfo& info) { m_getByIdExceptionInfo.append(info); }
333
334 size_t numberOfLineInfos() const { return m_lineInfo.size(); }
335 void addLineInfo(const LineInfo& lineInfo) { return m_lineInfo.append(lineInfo); }
336 LineInfo& lastLineInfo() { return m_lineInfo.last(); }
337
338#if !ENABLE(JIT)
339 void addPropertyAccessInstruction(unsigned propertyAccessInstruction) { m_propertyAccessInstructions.append(propertyAccessInstruction); }
340 void addGlobalResolveInstruction(unsigned globalResolveInstructions) { m_globalResolveInstructions.append(globalResolveInstructions); }
341#else
342 size_t numberOfStructureStubInfos() const { return m_structureStubInfos.size(); }
343 void addStructureStubInfo(const StructureStubInfo& stubInfo) { m_structureStubInfos.append(stubInfo); }
344 StructureStubInfo& structureStubInfo(int index) { return m_structureStubInfos[index]; }
345
346 void addGlobalResolveInfo() { m_globalResolveInfos.append(GlobalResolveInfo()); }
347 GlobalResolveInfo& globalResolveInfo(int index) { return m_globalResolveInfos[index]; }
348
349 size_t numberOfCallLinkInfos() const { return m_callLinkInfos.size(); }
350 void addCallLinkInfo() { m_callLinkInfos.append(CallLinkInfo()); }
351 CallLinkInfo& callLinkInfo(int index) { return m_callLinkInfos[index]; }
352
353 Vector<PC>& pcVector() { return m_pcVector; }
354#endif
355
356 // Constant Pool
357
358 size_t numberOfIdentifiers() const { return m_identifiers.size(); }
359 void addIdentifier(const Identifier& i) { return m_identifiers.append(i); }
360 Identifier& identifier(int index) { return m_identifiers[index]; }
361
362 size_t numberOfConstantRegisters() const { return m_constantRegisters.size(); }
363 void addConstantRegister(const Register& r) { return m_constantRegisters.append(r); }
364 Register& constantRegister(int index) { return m_constantRegisters[index]; }
365
366 unsigned addFunctionExpression(FuncExprNode* n) { unsigned size = m_functionExpressions.size(); m_functionExpressions.append(n); return size; }
367 FuncExprNode* functionExpression(int index) const { return m_functionExpressions[index].get(); }
368
369 unsigned addFunction(FuncDeclNode* n) { createRareDataIfNecessary(); unsigned size = m_rareData->m_functions.size(); m_rareData->m_functions.append(n); return size; }
370 FuncDeclNode* function(int index) const { ASSERT(m_rareData); return m_rareData->m_functions[index].get(); }
371
372 unsigned addUnexpectedConstant(JSValue* v) { createRareDataIfNecessary(); unsigned size = m_rareData->m_unexpectedConstants.size(); m_rareData->m_unexpectedConstants.append(v); return size; }
373 JSValue* unexpectedConstant(int index) const { ASSERT(m_rareData); return m_rareData->m_unexpectedConstants[index]; }
374
375 unsigned addRegExp(RegExp* r) { createRareDataIfNecessary(); unsigned size = m_rareData->m_regexps.size(); m_rareData->m_regexps.append(r); return size; }
376 RegExp* regexp(int index) const { ASSERT(m_rareData); return m_rareData->m_regexps[index].get(); }
377
378
379 // Jump Tables
380
381 size_t numberOfImmediateSwitchJumpTables() const { return m_rareData ? m_rareData->m_immediateSwitchJumpTables.size() : 0; }
382 SimpleJumpTable& addImmediateSwitchJumpTable() { createRareDataIfNecessary(); m_rareData->m_immediateSwitchJumpTables.append(SimpleJumpTable()); return m_rareData->m_immediateSwitchJumpTables.last(); }
383 SimpleJumpTable& immediateSwitchJumpTable(int tableIndex) { ASSERT(m_rareData); return m_rareData->m_immediateSwitchJumpTables[tableIndex]; }
384
385 size_t numberOfCharacterSwitchJumpTables() const { return m_rareData ? m_rareData->m_characterSwitchJumpTables.size() : 0; }
386 SimpleJumpTable& addCharacterSwitchJumpTable() { createRareDataIfNecessary(); m_rareData->m_characterSwitchJumpTables.append(SimpleJumpTable()); return m_rareData->m_characterSwitchJumpTables.last(); }
387 SimpleJumpTable& characterSwitchJumpTable(int tableIndex) { ASSERT(m_rareData); return m_rareData->m_characterSwitchJumpTables[tableIndex]; }
388
389 size_t numberOfStringSwitchJumpTables() const { return m_rareData ? m_rareData->m_stringSwitchJumpTables.size() : 0; }
390 StringJumpTable& addStringSwitchJumpTable() { createRareDataIfNecessary(); m_rareData->m_stringSwitchJumpTables.append(StringJumpTable()); return m_rareData->m_stringSwitchJumpTables.last(); }
391 StringJumpTable& stringSwitchJumpTable(int tableIndex) { ASSERT(m_rareData); return m_rareData->m_stringSwitchJumpTables[tableIndex]; }
392
393
394 SymbolTable& symbolTable() { return m_symbolTable; }
395
396 EvalCodeCache& evalCodeCache() { createRareDataIfNecessary(); return m_rareData->m_evalCodeCache; }
397
398 void shrinkToFit();
399
400 // FIXME: Make these remaining members private.
401
402 int m_numCalleeRegisters;
403 // NOTE: numConstants holds the number of constant registers allocated
404 // by the code generator, not the number of constant registers used.
405 // (Duplicate constants are uniqued during code generation, and spare
406 // constant registers may be allocated.)
407 int m_numConstants;
408 int m_numVars;
409 int m_numParameters;
410
411 private:
412#if !defined(NDEBUG) || ENABLE(OPCODE_SAMPLING)
413 void dump(ExecState*, const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator&) const;
414#endif
415
416 void createRareDataIfNecessary()
417 {
418 if (!m_rareData)
419 m_rareData.set(new RareData);
420 }
421
422
423 ScopeNode* m_ownerNode;
424 JSGlobalData* m_globalData;
425
426 Vector<Instruction> m_instructions;
427#if ENABLE(JIT)
428 JITCodeRef m_jitCode;
429#endif
430
431 int m_thisRegister;
432
433 bool m_needsFullScopeChain;
434 bool m_usesEval;
435 bool m_usesArguments;
436
437 CodeType m_codeType;
438
439 RefPtr<SourceProvider> m_source;
440 unsigned m_sourceOffset;
441
442#if !ENABLE(JIT)
443 Vector<unsigned> m_propertyAccessInstructions;
444 Vector<unsigned> m_globalResolveInstructions;
445#else
446 Vector<StructureStubInfo> m_structureStubInfos;
447 Vector<GlobalResolveInfo> m_globalResolveInfos;
448 Vector<CallLinkInfo> m_callLinkInfos;
449 Vector<CallLinkInfo*> m_linkedCallerList;
450#endif
451
452 Vector<unsigned> m_jumpTargets;
453
454 Vector<ExpressionRangeInfo> m_expressionInfo;
455 Vector<LineInfo> m_lineInfo;
456 Vector<GetByIdExceptionInfo> m_getByIdExceptionInfo;
457
458#if ENABLE(JIT)
459 Vector<PC> m_pcVector;
460#endif
461
462 // Constant Pool
463 Vector<Identifier> m_identifiers;
464 Vector<Register> m_constantRegisters;
465 Vector<RefPtr<FuncExprNode> > m_functionExpressions;
466
467 SymbolTable m_symbolTable;
468
469 struct RareData {
470 Vector<HandlerInfo> m_exceptionHandlers;
471
472 // Rare Constants
473 Vector<RefPtr<FuncDeclNode> > m_functions;
474 Vector<JSValue*> m_unexpectedConstants;
475 Vector<RefPtr<RegExp> > m_regexps;
476
477 // Jump Tables
478 Vector<SimpleJumpTable> m_immediateSwitchJumpTables;
479 Vector<SimpleJumpTable> m_characterSwitchJumpTables;
480 Vector<StringJumpTable> m_stringSwitchJumpTables;
481
482 EvalCodeCache m_evalCodeCache;
483 };
484
485 OwnPtr<RareData> m_rareData;
486 };
487
488 // Program code is not marked by any function, so we make the global object
489 // responsible for marking it.
490
491 class ProgramCodeBlock : public CodeBlock {
492 public:
493 ProgramCodeBlock(ScopeNode* ownerNode, CodeType codeType, JSGlobalObject* globalObject, PassRefPtr<SourceProvider> sourceProvider)
494 : CodeBlock(ownerNode, codeType, sourceProvider, 0)
495 , m_globalObject(globalObject)
496 {
497 m_globalObject->codeBlocks().add(this);
498 }
499
500 ~ProgramCodeBlock()
501 {
502 if (m_globalObject)
503 m_globalObject->codeBlocks().remove(this);
504 }
505
506 void clearGlobalObject() { m_globalObject = 0; }
507
508 private:
509 JSGlobalObject* m_globalObject; // For program and eval nodes, the global object that marks the constant pool.
510 };
511
512 class EvalCodeBlock : public ProgramCodeBlock {
513 public:
514 EvalCodeBlock(ScopeNode* ownerNode, JSGlobalObject* globalObject, PassRefPtr<SourceProvider> sourceProvider)
515 : ProgramCodeBlock(ownerNode, EvalCode, globalObject, sourceProvider)
516 {
517 }
518 };
519
520} // namespace JSC
521
522#endif // CodeBlock_h
Note: See TracBrowser for help on using the repository browser.