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