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