OSDN Git Service

68acae439951c3b69840e137ca90eb09b7e1b423
[android-x86/external-webkit.git] / JavaScriptCore / bytecode / CodeBlock.h
1 /*
2  * Copyright (C) 2008, 2009, 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Cameron Zwarich <cwzwarich@uwaterloo.ca>
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 "JITCode.h"
36 #include "JSGlobalObject.h"
37 #include "JumpTable.h"
38 #include "Nodes.h"
39 #include "RegExp.h"
40 #include "UString.h"
41 #include <wtf/FastAllocBase.h>
42 #include <wtf/PassOwnPtr.h>
43 #include <wtf/RefPtr.h>
44 #include <wtf/Vector.h>
45
46 #if ENABLE(JIT)
47 #include "StructureStubInfo.h"
48 #endif
49
50 // Register numbers used in bytecode operations have different meaning according to their ranges:
51 //      0x80000000-0xFFFFFFFF  Negative indices from the CallFrame pointer are entries in the call frame, see RegisterFile.h.
52 //      0x00000000-0x3FFFFFFF  Forwards indices from the CallFrame pointer are local vars and temporaries with the function's callframe.
53 //      0x40000000-0x7FFFFFFF  Positive indices from 0x40000000 specify entries in the constant pool on the CodeBlock.
54 static const int FirstConstantRegisterIndex = 0x40000000;
55
56 namespace JSC {
57
58     enum HasSeenShouldRepatch {
59         hasSeenShouldRepatch
60     };
61
62     class ExecState;
63
64     enum CodeType { GlobalCode, EvalCode, FunctionCode };
65
66     inline int unmodifiedArgumentsRegister(int argumentsRegister) { return argumentsRegister - 1; }
67
68     static ALWAYS_INLINE int missingThisObjectMarker() { return std::numeric_limits<int>::max(); }
69
70     struct HandlerInfo {
71         uint32_t start;
72         uint32_t end;
73         uint32_t target;
74         uint32_t scopeDepth;
75 #if ENABLE(JIT)
76         CodeLocationLabel nativeCode;
77 #endif
78     };
79
80     struct ExpressionRangeInfo {
81         enum {
82             MaxOffset = (1 << 7) - 1, 
83             MaxDivot = (1 << 25) - 1
84         };
85         uint32_t instructionOffset : 25;
86         uint32_t divotPoint : 25;
87         uint32_t startOffset : 7;
88         uint32_t endOffset : 7;
89     };
90
91     struct LineInfo {
92         uint32_t instructionOffset;
93         int32_t lineNumber;
94     };
95
96     // Both op_construct and op_instanceof require a use of op_get_by_id to get
97     // the prototype property from an object. The exception messages for exceptions
98     // thrown by these instances op_get_by_id need to reflect this.
99     struct GetByIdExceptionInfo {
100         unsigned bytecodeOffset : 31;
101         bool isOpCreateThis : 1;
102     };
103
104 #if ENABLE(JIT)
105     struct CallLinkInfo {
106         CallLinkInfo()
107             : callee(0)
108             , position(0)
109             , hasSeenShouldRepatch(0)
110         {
111         }
112
113         unsigned bytecodeOffset;
114         CodeLocationNearCall callReturnLocation;
115         CodeLocationDataLabelPtr hotPathBegin;
116         CodeLocationNearCall hotPathOther;
117         CodeBlock* ownerCodeBlock;
118         CodeBlock* callee;
119         unsigned position : 31;
120         unsigned hasSeenShouldRepatch : 1;
121         
122         void setUnlinked() { callee = 0; }
123         bool isLinked() { return callee; }
124
125         bool seenOnce()
126         {
127             return hasSeenShouldRepatch;
128         }
129
130         void setSeen()
131         {
132             hasSeenShouldRepatch = true;
133         }
134     };
135
136     struct MethodCallLinkInfo {
137         MethodCallLinkInfo()
138             : cachedStructure(0)
139             , cachedPrototypeStructure(0)
140         {
141         }
142
143         bool seenOnce()
144         {
145             ASSERT(!cachedStructure);
146             return cachedPrototypeStructure;
147         }
148
149         void setSeen()
150         {
151             ASSERT(!cachedStructure && !cachedPrototypeStructure);
152             // We use the values of cachedStructure & cachedPrototypeStructure to indicate the
153             // current state.
154             //     - In the initial state, both are null.
155             //     - Once this transition has been taken once, cachedStructure is
156             //       null and cachedPrototypeStructure is set to a nun-null value.
157             //     - Once the call is linked both structures are set to non-null values.
158             cachedPrototypeStructure = (Structure*)1;
159         }
160
161         CodeLocationCall callReturnLocation;
162         CodeLocationDataLabelPtr structureLabel;
163         Structure* cachedStructure;
164         Structure* cachedPrototypeStructure;
165     };
166
167     struct FunctionRegisterInfo {
168         FunctionRegisterInfo(unsigned bytecodeOffset, int functionRegisterIndex)
169             : bytecodeOffset(bytecodeOffset)
170             , functionRegisterIndex(functionRegisterIndex)
171         {
172         }
173
174         unsigned bytecodeOffset;
175         int functionRegisterIndex;
176     };
177
178     struct GlobalResolveInfo {
179         GlobalResolveInfo(unsigned bytecodeOffset)
180             : structure(0)
181             , offset(0)
182             , bytecodeOffset(bytecodeOffset)
183         {
184         }
185
186         Structure* structure;
187         unsigned offset;
188         unsigned bytecodeOffset;
189     };
190
191     // This structure is used to map from a call return location
192     // (given as an offset in bytes into the JIT code) back to
193     // the bytecode index of the corresponding bytecode operation.
194     // This is then used to look up the corresponding handler.
195     struct CallReturnOffsetToBytecodeOffset {
196         CallReturnOffsetToBytecodeOffset(unsigned callReturnOffset, unsigned bytecodeOffset)
197             : callReturnOffset(callReturnOffset)
198             , bytecodeOffset(bytecodeOffset)
199         {
200         }
201
202         unsigned callReturnOffset;
203         unsigned bytecodeOffset;
204     };
205
206     // valueAtPosition helpers for the binaryChop algorithm below.
207
208     inline void* getStructureStubInfoReturnLocation(StructureStubInfo* structureStubInfo)
209     {
210         return structureStubInfo->callReturnLocation.executableAddress();
211     }
212
213     inline void* getCallLinkInfoReturnLocation(CallLinkInfo* callLinkInfo)
214     {
215         return callLinkInfo->callReturnLocation.executableAddress();
216     }
217
218     inline void* getMethodCallLinkInfoReturnLocation(MethodCallLinkInfo* methodCallLinkInfo)
219     {
220         return methodCallLinkInfo->callReturnLocation.executableAddress();
221     }
222
223     inline unsigned getCallReturnOffset(CallReturnOffsetToBytecodeOffset* pc)
224     {
225         return pc->callReturnOffset;
226     }
227
228     // Binary chop algorithm, calls valueAtPosition on pre-sorted elements in array,
229     // compares result with key (KeyTypes should be comparable with '--', '<', '>').
230     // Optimized for cases where the array contains the key, checked by assertions.
231     template<typename ArrayType, typename KeyType, KeyType(*valueAtPosition)(ArrayType*)>
232     inline ArrayType* binaryChop(ArrayType* array, size_t size, KeyType key)
233     {
234         // The array must contain at least one element (pre-condition, array does conatin key).
235         // If the array only contains one element, no need to do the comparison.
236         while (size > 1) {
237             // Pick an element to check, half way through the array, and read the value.
238             int pos = (size - 1) >> 1;
239             KeyType val = valueAtPosition(&array[pos]);
240             
241             // If the key matches, success!
242             if (val == key)
243                 return &array[pos];
244             // The item we are looking for is smaller than the item being check; reduce the value of 'size',
245             // chopping off the right hand half of the array.
246             else if (key < val)
247                 size = pos;
248             // Discard all values in the left hand half of the array, up to and including the item at pos.
249             else {
250                 size -= (pos + 1);
251                 array += (pos + 1);
252             }
253
254             // 'size' should never reach zero.
255             ASSERT(size);
256         }
257         
258         // If we reach this point we've chopped down to one element, no need to check it matches
259         ASSERT(size == 1);
260         ASSERT(key == valueAtPosition(&array[0]));
261         return &array[0];
262     }
263 #endif
264
265     struct ExceptionInfo : FastAllocBase {
266         Vector<ExpressionRangeInfo> m_expressionInfo;
267         Vector<LineInfo> m_lineInfo;
268         Vector<GetByIdExceptionInfo> m_getByIdExceptionInfo;
269
270 #if ENABLE(JIT)
271         Vector<CallReturnOffsetToBytecodeOffset> m_callReturnIndexVector;
272 #endif
273     };
274
275     class CodeBlock : public FastAllocBase {
276         friend class JIT;
277     protected:
278         CodeBlock(ScriptExecutable* ownerExecutable, CodeType, JSGlobalObject*, PassRefPtr<SourceProvider>, unsigned sourceOffset, SymbolTable* symbolTable, bool isConstructor);
279
280         JSGlobalObject* m_globalObject;
281
282     public:
283         virtual ~CodeBlock();
284
285         void markAggregate(MarkStack&);
286         void refStructures(Instruction* vPC) const;
287         void derefStructures(Instruction* vPC) const;
288 #if ENABLE(JIT_OPTIMIZE_CALL)
289         void unlinkCallers();
290 #endif
291
292         static void dumpStatistics();
293
294 #if !defined(NDEBUG) || ENABLE_OPCODE_SAMPLING
295         void dump(ExecState*) const;
296         void printStructures(const Instruction*) const;
297         void printStructure(const char* name, const Instruction*, int operand) const;
298 #endif
299
300         bool isStrictMode() const { return m_isStrictMode; }
301
302         inline bool isKnownNotImmediate(int index)
303         {
304             if (index == m_thisRegister && !m_isStrictMode)
305                 return true;
306
307             if (isConstantRegisterIndex(index))
308                 return getConstant(index).isCell();
309
310             return false;
311         }
312
313         ALWAYS_INLINE bool isTemporaryRegisterIndex(int index)
314         {
315             return index >= m_numVars;
316         }
317
318         HandlerInfo* handlerForBytecodeOffset(unsigned bytecodeOffset);
319         int lineNumberForBytecodeOffset(CallFrame*, unsigned bytecodeOffset);
320         int expressionRangeForBytecodeOffset(CallFrame*, unsigned bytecodeOffset, int& divot, int& startOffset, int& endOffset);
321         bool getByIdExceptionInfoForBytecodeOffset(CallFrame*, unsigned bytecodeOffset, OpcodeID&);
322
323 #if ENABLE(JIT)
324         void addCaller(CallLinkInfo* caller)
325         {
326             caller->callee = this;
327             caller->position = m_linkedCallerList.size();
328             m_linkedCallerList.append(caller);
329         }
330
331         void removeCaller(CallLinkInfo* caller)
332         {
333             unsigned pos = caller->position;
334             unsigned lastPos = m_linkedCallerList.size() - 1;
335
336             if (pos != lastPos) {
337                 m_linkedCallerList[pos] = m_linkedCallerList[lastPos];
338                 m_linkedCallerList[pos]->position = pos;
339             }
340             m_linkedCallerList.shrink(lastPos);
341         }
342
343         StructureStubInfo& getStubInfo(ReturnAddressPtr returnAddress)
344         {
345             return *(binaryChop<StructureStubInfo, void*, getStructureStubInfoReturnLocation>(m_structureStubInfos.begin(), m_structureStubInfos.size(), returnAddress.value()));
346         }
347
348         CallLinkInfo& getCallLinkInfo(ReturnAddressPtr returnAddress)
349         {
350             return *(binaryChop<CallLinkInfo, void*, getCallLinkInfoReturnLocation>(m_callLinkInfos.begin(), m_callLinkInfos.size(), returnAddress.value()));
351         }
352
353         MethodCallLinkInfo& getMethodCallLinkInfo(ReturnAddressPtr returnAddress)
354         {
355             return *(binaryChop<MethodCallLinkInfo, void*, getMethodCallLinkInfoReturnLocation>(m_methodCallLinkInfos.begin(), m_methodCallLinkInfos.size(), returnAddress.value()));
356         }
357
358         unsigned bytecodeOffset(CallFrame* callFrame, ReturnAddressPtr returnAddress)
359         {
360             if (!reparseForExceptionInfoIfNecessary(callFrame))
361                 return 1;
362             return binaryChop<CallReturnOffsetToBytecodeOffset, unsigned, getCallReturnOffset>(callReturnIndexVector().begin(), callReturnIndexVector().size(), getJITCode().offsetOf(returnAddress.value()))->bytecodeOffset;
363         }
364         
365         bool functionRegisterForBytecodeOffset(unsigned bytecodeOffset, int& functionRegisterIndex);
366 #endif
367 #if ENABLE(INTERPRETER)
368         unsigned bytecodeOffset(CallFrame*, Instruction* returnAddress)
369         {
370             return static_cast<Instruction*>(returnAddress) - instructions().begin();
371         }
372 #endif
373
374         void setIsNumericCompareFunction(bool isNumericCompareFunction) { m_isNumericCompareFunction = isNumericCompareFunction; }
375         bool isNumericCompareFunction() { return m_isNumericCompareFunction; }
376
377         Vector<Instruction>& instructions() { return m_instructions; }
378         void discardBytecode() { m_instructions.clear(); }
379
380 #ifndef NDEBUG
381         unsigned instructionCount() { return m_instructionCount; }
382         void setInstructionCount(unsigned instructionCount) { m_instructionCount = instructionCount; }
383 #endif
384
385 #if ENABLE(JIT)
386         JITCode& getJITCode() { return m_isConstructor ? ownerExecutable()->generatedJITCodeForConstruct() : ownerExecutable()->generatedJITCodeForCall(); }
387         ExecutablePool* executablePool() { return getJITCode().getExecutablePool(); }
388 #endif
389
390         ScriptExecutable* ownerExecutable() const { return m_ownerExecutable; }
391
392         void setGlobalData(JSGlobalData* globalData) { m_globalData = globalData; }
393
394         void setThisRegister(int thisRegister) { m_thisRegister = thisRegister; }
395         int thisRegister() const { return m_thisRegister; }
396
397         void setNeedsFullScopeChain(bool needsFullScopeChain) { m_needsFullScopeChain = needsFullScopeChain; }
398         bool needsFullScopeChain() const { return m_needsFullScopeChain; }
399         void setUsesEval(bool usesEval) { m_usesEval = usesEval; }
400         bool usesEval() const { return m_usesEval; }
401         
402         void setArgumentsRegister(int argumentsRegister)
403         {
404             ASSERT(argumentsRegister != -1);
405             m_argumentsRegister = argumentsRegister;
406             ASSERT(usesArguments());
407         }
408         int argumentsRegister()
409         {
410             ASSERT(usesArguments());
411             return m_argumentsRegister;
412         }
413         void setActivationRegister(int activationRegister)
414         {
415             m_activationRegister = activationRegister;
416         }
417         int activationRegister()
418         {
419             ASSERT(needsFullScopeChain());
420             return m_activationRegister;
421         }
422         bool usesArguments() const { return m_argumentsRegister != -1; }
423
424         CodeType codeType() const { return m_codeType; }
425
426         SourceProvider* source() const { return m_source.get(); }
427         unsigned sourceOffset() const { return m_sourceOffset; }
428
429         size_t numberOfJumpTargets() const { return m_jumpTargets.size(); }
430         void addJumpTarget(unsigned jumpTarget) { m_jumpTargets.append(jumpTarget); }
431         unsigned jumpTarget(int index) const { return m_jumpTargets[index]; }
432         unsigned lastJumpTarget() const { return m_jumpTargets.last(); }
433
434         void createActivation(CallFrame*);
435
436 #if ENABLE(INTERPRETER)
437         void addPropertyAccessInstruction(unsigned propertyAccessInstruction) { m_propertyAccessInstructions.append(propertyAccessInstruction); }
438         void addGlobalResolveInstruction(unsigned globalResolveInstruction) { m_globalResolveInstructions.append(globalResolveInstruction); }
439         bool hasGlobalResolveInstructionAtBytecodeOffset(unsigned bytecodeOffset);
440 #endif
441 #if ENABLE(JIT)
442         size_t numberOfStructureStubInfos() const { return m_structureStubInfos.size(); }
443         void addStructureStubInfo(const StructureStubInfo& stubInfo) { m_structureStubInfos.append(stubInfo); }
444         StructureStubInfo& structureStubInfo(int index) { return m_structureStubInfos[index]; }
445
446         void addGlobalResolveInfo(unsigned globalResolveInstruction) { m_globalResolveInfos.append(GlobalResolveInfo(globalResolveInstruction)); }
447         GlobalResolveInfo& globalResolveInfo(int index) { return m_globalResolveInfos[index]; }
448         bool hasGlobalResolveInfoAtBytecodeOffset(unsigned bytecodeOffset);
449
450         size_t numberOfCallLinkInfos() const { return m_callLinkInfos.size(); }
451         void addCallLinkInfo() { m_callLinkInfos.append(CallLinkInfo()); }
452         CallLinkInfo& callLinkInfo(int index) { return m_callLinkInfos[index]; }
453
454         void addMethodCallLinkInfos(unsigned n) { m_methodCallLinkInfos.grow(n); }
455         MethodCallLinkInfo& methodCallLinkInfo(int index) { return m_methodCallLinkInfos[index]; }
456
457         void addFunctionRegisterInfo(unsigned bytecodeOffset, int functionIndex) { createRareDataIfNecessary(); m_rareData->m_functionRegisterInfos.append(FunctionRegisterInfo(bytecodeOffset, functionIndex)); }
458 #endif
459
460         // Exception handling support
461
462         size_t numberOfExceptionHandlers() const { return m_rareData ? m_rareData->m_exceptionHandlers.size() : 0; }
463         void addExceptionHandler(const HandlerInfo& hanler) { createRareDataIfNecessary(); return m_rareData->m_exceptionHandlers.append(hanler); }
464         HandlerInfo& exceptionHandler(int index) { ASSERT(m_rareData); return m_rareData->m_exceptionHandlers[index]; }
465
466         bool hasExceptionInfo() const { return m_exceptionInfo; }
467         void clearExceptionInfo() { m_exceptionInfo.clear(); }
468         PassOwnPtr<ExceptionInfo> extractExceptionInfo();
469
470         void addExpressionInfo(const ExpressionRangeInfo& expressionInfo) { ASSERT(m_exceptionInfo); m_exceptionInfo->m_expressionInfo.append(expressionInfo); }
471         void addGetByIdExceptionInfo(const GetByIdExceptionInfo& info) { ASSERT(m_exceptionInfo); m_exceptionInfo->m_getByIdExceptionInfo.append(info); }
472
473         size_t numberOfLineInfos() const { ASSERT(m_exceptionInfo); return m_exceptionInfo->m_lineInfo.size(); }
474         void addLineInfo(const LineInfo& lineInfo) { ASSERT(m_exceptionInfo); m_exceptionInfo->m_lineInfo.append(lineInfo); }
475         LineInfo& lastLineInfo() { ASSERT(m_exceptionInfo); return m_exceptionInfo->m_lineInfo.last(); }
476
477 #if ENABLE(JIT)
478         Vector<CallReturnOffsetToBytecodeOffset>& callReturnIndexVector() { ASSERT(m_exceptionInfo); return m_exceptionInfo->m_callReturnIndexVector; }
479 #endif
480
481         // Constant Pool
482
483         size_t numberOfIdentifiers() const { return m_identifiers.size(); }
484         void addIdentifier(const Identifier& i) { return m_identifiers.append(i); }
485         Identifier& identifier(int index) { return m_identifiers[index]; }
486
487         size_t numberOfConstantRegisters() const { return m_constantRegisters.size(); }
488         void addConstantRegister(const Register& r) { return m_constantRegisters.append(r); }
489         Register& constantRegister(int index) { return m_constantRegisters[index - FirstConstantRegisterIndex]; }
490         ALWAYS_INLINE bool isConstantRegisterIndex(int index) const { return index >= FirstConstantRegisterIndex; }
491         ALWAYS_INLINE JSValue getConstant(int index) const { return m_constantRegisters[index - FirstConstantRegisterIndex].jsValue(); }
492
493         unsigned addFunctionDecl(NonNullPassRefPtr<FunctionExecutable> n) { unsigned size = m_functionDecls.size(); m_functionDecls.append(n); return size; }
494         FunctionExecutable* functionDecl(int index) { return m_functionDecls[index].get(); }
495         int numberOfFunctionDecls() { return m_functionDecls.size(); }
496         unsigned addFunctionExpr(NonNullPassRefPtr<FunctionExecutable> n) { unsigned size = m_functionExprs.size(); m_functionExprs.append(n); return size; }
497         FunctionExecutable* functionExpr(int index) { return m_functionExprs[index].get(); }
498
499         unsigned addRegExp(RegExp* r) { createRareDataIfNecessary(); unsigned size = m_rareData->m_regexps.size(); m_rareData->m_regexps.append(r); return size; }
500         RegExp* regexp(int index) const { ASSERT(m_rareData); return m_rareData->m_regexps[index].get(); }
501
502         JSGlobalObject* globalObject() { return m_globalObject; }
503
504         // Jump Tables
505
506         size_t numberOfImmediateSwitchJumpTables() const { return m_rareData ? m_rareData->m_immediateSwitchJumpTables.size() : 0; }
507         SimpleJumpTable& addImmediateSwitchJumpTable() { createRareDataIfNecessary(); m_rareData->m_immediateSwitchJumpTables.append(SimpleJumpTable()); return m_rareData->m_immediateSwitchJumpTables.last(); }
508         SimpleJumpTable& immediateSwitchJumpTable(int tableIndex) { ASSERT(m_rareData); return m_rareData->m_immediateSwitchJumpTables[tableIndex]; }
509
510         size_t numberOfCharacterSwitchJumpTables() const { return m_rareData ? m_rareData->m_characterSwitchJumpTables.size() : 0; }
511         SimpleJumpTable& addCharacterSwitchJumpTable() { createRareDataIfNecessary(); m_rareData->m_characterSwitchJumpTables.append(SimpleJumpTable()); return m_rareData->m_characterSwitchJumpTables.last(); }
512         SimpleJumpTable& characterSwitchJumpTable(int tableIndex) { ASSERT(m_rareData); return m_rareData->m_characterSwitchJumpTables[tableIndex]; }
513
514         size_t numberOfStringSwitchJumpTables() const { return m_rareData ? m_rareData->m_stringSwitchJumpTables.size() : 0; }
515         StringJumpTable& addStringSwitchJumpTable() { createRareDataIfNecessary(); m_rareData->m_stringSwitchJumpTables.append(StringJumpTable()); return m_rareData->m_stringSwitchJumpTables.last(); }
516         StringJumpTable& stringSwitchJumpTable(int tableIndex) { ASSERT(m_rareData); return m_rareData->m_stringSwitchJumpTables[tableIndex]; }
517
518
519         SymbolTable* symbolTable() { return m_symbolTable; }
520         SharedSymbolTable* sharedSymbolTable() { ASSERT(m_codeType == FunctionCode); return static_cast<SharedSymbolTable*>(m_symbolTable); }
521
522         EvalCodeCache& evalCodeCache() { createRareDataIfNecessary(); return m_rareData->m_evalCodeCache; }
523
524         void shrinkToFit();
525
526         // FIXME: Make these remaining members private.
527
528         int m_numCalleeRegisters;
529         int m_numVars;
530         int m_numCapturedVars;
531         int m_numParameters;
532         bool m_isConstructor;
533
534     private:
535 #if !defined(NDEBUG) || ENABLE(OPCODE_SAMPLING)
536         void dump(ExecState*, const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator&) const;
537
538         CString registerName(ExecState*, int r) const;
539         void printUnaryOp(ExecState*, int location, Vector<Instruction>::const_iterator&, const char* op) const;
540         void printBinaryOp(ExecState*, int location, Vector<Instruction>::const_iterator&, const char* op) const;
541         void printConditionalJump(ExecState*, const Vector<Instruction>::const_iterator&, Vector<Instruction>::const_iterator&, int location, const char* op) const;
542         void printGetByIdOp(ExecState*, int location, Vector<Instruction>::const_iterator&, const char* op) const;
543         void printPutByIdOp(ExecState*, int location, Vector<Instruction>::const_iterator&, const char* op) const;
544 #endif
545
546         bool reparseForExceptionInfoIfNecessary(CallFrame*) WARN_UNUSED_RETURN;
547
548         void createRareDataIfNecessary()
549         {
550             if (!m_rareData)
551                 m_rareData = adoptPtr(new RareData);
552         }
553
554         ScriptExecutable* m_ownerExecutable;
555         JSGlobalData* m_globalData;
556
557         Vector<Instruction> m_instructions;
558 #ifndef NDEBUG
559         unsigned m_instructionCount;
560 #endif
561
562         int m_thisRegister;
563         int m_argumentsRegister;
564         int m_activationRegister;
565
566         bool m_needsFullScopeChain;
567         bool m_usesEval;
568         bool m_isNumericCompareFunction;
569         bool m_isStrictMode;
570
571         CodeType m_codeType;
572
573         RefPtr<SourceProvider> m_source;
574         unsigned m_sourceOffset;
575
576 #if ENABLE(INTERPRETER)
577         Vector<unsigned> m_propertyAccessInstructions;
578         Vector<unsigned> m_globalResolveInstructions;
579 #endif
580 #if ENABLE(JIT)
581         Vector<StructureStubInfo> m_structureStubInfos;
582         Vector<GlobalResolveInfo> m_globalResolveInfos;
583         Vector<CallLinkInfo> m_callLinkInfos;
584         Vector<MethodCallLinkInfo> m_methodCallLinkInfos;
585         Vector<CallLinkInfo*> m_linkedCallerList;
586 #endif
587
588         Vector<unsigned> m_jumpTargets;
589
590         // Constant Pool
591         Vector<Identifier> m_identifiers;
592         Vector<Register> m_constantRegisters;
593         Vector<RefPtr<FunctionExecutable> > m_functionDecls;
594         Vector<RefPtr<FunctionExecutable> > m_functionExprs;
595
596         SymbolTable* m_symbolTable;
597
598         OwnPtr<ExceptionInfo> m_exceptionInfo;
599
600         struct RareData : FastAllocBase {
601             Vector<HandlerInfo> m_exceptionHandlers;
602
603             // Rare Constants
604             Vector<RefPtr<RegExp> > m_regexps;
605
606             // Jump Tables
607             Vector<SimpleJumpTable> m_immediateSwitchJumpTables;
608             Vector<SimpleJumpTable> m_characterSwitchJumpTables;
609             Vector<StringJumpTable> m_stringSwitchJumpTables;
610
611             EvalCodeCache m_evalCodeCache;
612
613 #if ENABLE(JIT)
614             Vector<FunctionRegisterInfo> m_functionRegisterInfos;
615 #endif
616         };
617         OwnPtr<RareData> m_rareData;
618     };
619
620     // Program code is not marked by any function, so we make the global object
621     // responsible for marking it.
622
623     class GlobalCodeBlock : public CodeBlock {
624     public:
625         GlobalCodeBlock(ScriptExecutable* ownerExecutable, CodeType codeType, JSGlobalObject* globalObject, PassRefPtr<SourceProvider> sourceProvider, unsigned sourceOffset)
626             : CodeBlock(ownerExecutable, codeType, globalObject, sourceProvider, sourceOffset, &m_unsharedSymbolTable, false)
627         {
628             m_globalObject->codeBlocks().add(this);
629         }
630
631         ~GlobalCodeBlock()
632         {
633             if (m_globalObject)
634                 m_globalObject->codeBlocks().remove(this);
635         }
636
637         void clearGlobalObject() { m_globalObject = 0; }
638
639     private:
640         SymbolTable m_unsharedSymbolTable;
641     };
642
643     class ProgramCodeBlock : public GlobalCodeBlock {
644     public:
645         ProgramCodeBlock(ProgramExecutable* ownerExecutable, CodeType codeType, JSGlobalObject* globalObject, PassRefPtr<SourceProvider> sourceProvider)
646             : GlobalCodeBlock(ownerExecutable, codeType, globalObject, sourceProvider, 0)
647         {
648         }
649     };
650
651     class EvalCodeBlock : public GlobalCodeBlock {
652     public:
653         EvalCodeBlock(EvalExecutable* ownerExecutable, JSGlobalObject* globalObject, PassRefPtr<SourceProvider> sourceProvider, int baseScopeDepth)
654             : GlobalCodeBlock(ownerExecutable, EvalCode, globalObject, sourceProvider, 0)
655             , m_baseScopeDepth(baseScopeDepth)
656         {
657         }
658
659         int baseScopeDepth() const { return m_baseScopeDepth; }
660
661         const Identifier& variable(unsigned index) { return m_variables[index]; }
662         unsigned numVariables() { return m_variables.size(); }
663         void adoptVariables(Vector<Identifier>& variables)
664         {
665             ASSERT(m_variables.isEmpty());
666             m_variables.swap(variables);
667         }
668
669     private:
670         int m_baseScopeDepth;
671         Vector<Identifier> m_variables;
672     };
673
674     class FunctionCodeBlock : public CodeBlock {
675     public:
676         // Rather than using the usual RefCounted::create idiom for SharedSymbolTable we just use new
677         // as we need to initialise the CodeBlock before we could initialise any RefPtr to hold the shared
678         // symbol table, so we just pass as a raw pointer with a ref count of 1.  We then manually deref
679         // in the destructor.
680         FunctionCodeBlock(FunctionExecutable* ownerExecutable, CodeType codeType, JSGlobalObject* globalObject, PassRefPtr<SourceProvider> sourceProvider, unsigned sourceOffset, bool isConstructor)
681             : CodeBlock(ownerExecutable, codeType, globalObject, sourceProvider, sourceOffset, SharedSymbolTable::create().leakRef(), isConstructor)
682         {
683         }
684         ~FunctionCodeBlock()
685         {
686             sharedSymbolTable()->deref();
687         }
688     };
689
690     inline PassOwnPtr<ExceptionInfo> CodeBlock::extractExceptionInfo()
691     {
692         ASSERT(m_exceptionInfo);
693         return m_exceptionInfo.release();
694     }
695
696     inline Register& ExecState::r(int index)
697     {
698         CodeBlock* codeBlock = this->codeBlock();
699         if (codeBlock->isConstantRegisterIndex(index))
700             return codeBlock->constantRegister(index);
701         return this[index];
702     }
703
704 } // namespace JSC
705
706 #endif // CodeBlock_h