OSDN Git Service

Merge WebKit at r76408: Initial merge by git.
[android-x86/external-webkit.git] / Source / 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 #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         JSGlobalObject* m_globalObject;
252
253     public:
254         virtual ~CodeBlock();
255
256         void markAggregate(MarkStack&);
257         void refStructures(Instruction* vPC) const;
258         void derefStructures(Instruction* vPC) const;
259 #if ENABLE(JIT_OPTIMIZE_CALL)
260         void unlinkCallers();
261 #endif
262
263         static void dumpStatistics();
264
265 #if !defined(NDEBUG) || ENABLE_OPCODE_SAMPLING
266         void dump(ExecState*) const;
267         void printStructures(const Instruction*) const;
268         void printStructure(const char* name, const Instruction*, int operand) const;
269 #endif
270
271         bool isStrictMode() const { return m_isStrictMode; }
272
273         inline bool isKnownNotImmediate(int index)
274         {
275             if (index == m_thisRegister && !m_isStrictMode)
276                 return true;
277
278             if (isConstantRegisterIndex(index))
279                 return getConstant(index).isCell();
280
281             return false;
282         }
283
284         ALWAYS_INLINE bool isTemporaryRegisterIndex(int index)
285         {
286             return index >= m_numVars;
287         }
288
289         HandlerInfo* handlerForBytecodeOffset(unsigned bytecodeOffset);
290         int lineNumberForBytecodeOffset(unsigned bytecodeOffset);
291         void expressionRangeForBytecodeOffset(unsigned bytecodeOffset, int& divot, int& startOffset, int& endOffset);
292
293 #if ENABLE(JIT)
294         void addCaller(CallLinkInfo* caller)
295         {
296             caller->callee = this;
297             caller->position = m_linkedCallerList.size();
298             m_linkedCallerList.append(caller);
299         }
300
301         void removeCaller(CallLinkInfo* caller)
302         {
303             unsigned pos = caller->position;
304             unsigned lastPos = m_linkedCallerList.size() - 1;
305
306             if (pos != lastPos) {
307                 m_linkedCallerList[pos] = m_linkedCallerList[lastPos];
308                 m_linkedCallerList[pos]->position = pos;
309             }
310             m_linkedCallerList.shrink(lastPos);
311         }
312
313         StructureStubInfo& getStubInfo(ReturnAddressPtr returnAddress)
314         {
315             return *(binaryChop<StructureStubInfo, void*, getStructureStubInfoReturnLocation>(m_structureStubInfos.begin(), m_structureStubInfos.size(), returnAddress.value()));
316         }
317
318         CallLinkInfo& getCallLinkInfo(ReturnAddressPtr returnAddress)
319         {
320             return *(binaryChop<CallLinkInfo, void*, getCallLinkInfoReturnLocation>(m_callLinkInfos.begin(), m_callLinkInfos.size(), returnAddress.value()));
321         }
322
323         MethodCallLinkInfo& getMethodCallLinkInfo(ReturnAddressPtr returnAddress)
324         {
325             return *(binaryChop<MethodCallLinkInfo, void*, getMethodCallLinkInfoReturnLocation>(m_methodCallLinkInfos.begin(), m_methodCallLinkInfos.size(), returnAddress.value()));
326         }
327
328         unsigned bytecodeOffset(ReturnAddressPtr returnAddress)
329         {
330             if (!m_rareData)
331                 return 1;
332             Vector<CallReturnOffsetToBytecodeOffset>& callIndices = m_rareData->m_callReturnIndexVector;
333             if (!callIndices.size())
334                 return 1;
335             return binaryChop<CallReturnOffsetToBytecodeOffset, unsigned, getCallReturnOffset>(callIndices.begin(), callIndices.size(), getJITCode().offsetOf(returnAddress.value()))->bytecodeOffset;
336         }
337 #endif
338 #if ENABLE(INTERPRETER)
339         unsigned bytecodeOffset(Instruction* returnAddress)
340         {
341             return static_cast<Instruction*>(returnAddress) - instructions().begin();
342         }
343 #endif
344
345         void setIsNumericCompareFunction(bool isNumericCompareFunction) { m_isNumericCompareFunction = isNumericCompareFunction; }
346         bool isNumericCompareFunction() { return m_isNumericCompareFunction; }
347
348         Vector<Instruction>& instructions() { return m_instructions; }
349         void discardBytecode() { m_instructions.clear(); }
350
351 #ifndef NDEBUG
352         unsigned instructionCount() { return m_instructionCount; }
353         void setInstructionCount(unsigned instructionCount) { m_instructionCount = instructionCount; }
354 #endif
355
356 #if ENABLE(JIT)
357         JITCode& getJITCode() { return m_isConstructor ? ownerExecutable()->generatedJITCodeForConstruct() : ownerExecutable()->generatedJITCodeForCall(); }
358         ExecutablePool* executablePool() { return getJITCode().getExecutablePool(); }
359 #endif
360
361         ScriptExecutable* ownerExecutable() const { return m_ownerExecutable; }
362
363         void setGlobalData(JSGlobalData* globalData) { m_globalData = globalData; }
364
365         void setThisRegister(int thisRegister) { m_thisRegister = thisRegister; }
366         int thisRegister() const { return m_thisRegister; }
367
368         void setNeedsFullScopeChain(bool needsFullScopeChain) { m_needsFullScopeChain = needsFullScopeChain; }
369         bool needsFullScopeChain() const { return m_needsFullScopeChain; }
370         void setUsesEval(bool usesEval) { m_usesEval = usesEval; }
371         bool usesEval() const { return m_usesEval; }
372         
373         void setArgumentsRegister(int argumentsRegister)
374         {
375             ASSERT(argumentsRegister != -1);
376             m_argumentsRegister = argumentsRegister;
377             ASSERT(usesArguments());
378         }
379         int argumentsRegister()
380         {
381             ASSERT(usesArguments());
382             return m_argumentsRegister;
383         }
384         void setActivationRegister(int activationRegister)
385         {
386             m_activationRegister = activationRegister;
387         }
388         int activationRegister()
389         {
390             ASSERT(needsFullScopeChain());
391             return m_activationRegister;
392         }
393         bool usesArguments() const { return m_argumentsRegister != -1; }
394
395         CodeType codeType() const { return m_codeType; }
396
397         SourceProvider* source() const { return m_source.get(); }
398         unsigned sourceOffset() const { return m_sourceOffset; }
399
400         size_t numberOfJumpTargets() const { return m_jumpTargets.size(); }
401         void addJumpTarget(unsigned jumpTarget) { m_jumpTargets.append(jumpTarget); }
402         unsigned jumpTarget(int index) const { return m_jumpTargets[index]; }
403         unsigned lastJumpTarget() const { return m_jumpTargets.last(); }
404
405         void createActivation(CallFrame*);
406
407 #if ENABLE(INTERPRETER)
408         void addPropertyAccessInstruction(unsigned propertyAccessInstruction) { m_propertyAccessInstructions.append(propertyAccessInstruction); }
409         void addGlobalResolveInstruction(unsigned globalResolveInstruction) { m_globalResolveInstructions.append(globalResolveInstruction); }
410         bool hasGlobalResolveInstructionAtBytecodeOffset(unsigned bytecodeOffset);
411 #endif
412 #if ENABLE(JIT)
413         size_t numberOfStructureStubInfos() const { return m_structureStubInfos.size(); }
414         void addStructureStubInfo(const StructureStubInfo& stubInfo) { m_structureStubInfos.append(stubInfo); }
415         StructureStubInfo& structureStubInfo(int index) { return m_structureStubInfos[index]; }
416
417         void addGlobalResolveInfo(unsigned globalResolveInstruction) { m_globalResolveInfos.append(GlobalResolveInfo(globalResolveInstruction)); }
418         GlobalResolveInfo& globalResolveInfo(int index) { return m_globalResolveInfos[index]; }
419         bool hasGlobalResolveInfoAtBytecodeOffset(unsigned bytecodeOffset);
420
421         size_t numberOfCallLinkInfos() const { return m_callLinkInfos.size(); }
422         void addCallLinkInfo() { m_callLinkInfos.append(CallLinkInfo()); }
423         CallLinkInfo& callLinkInfo(int index) { return m_callLinkInfos[index]; }
424
425         void addMethodCallLinkInfos(unsigned n) { m_methodCallLinkInfos.grow(n); }
426         MethodCallLinkInfo& methodCallLinkInfo(int index) { return m_methodCallLinkInfos[index]; }
427 #endif
428
429         // Exception handling support
430
431         size_t numberOfExceptionHandlers() const { return m_rareData ? m_rareData->m_exceptionHandlers.size() : 0; }
432         void addExceptionHandler(const HandlerInfo& hanler) { createRareDataIfNecessary(); return m_rareData->m_exceptionHandlers.append(hanler); }
433         HandlerInfo& exceptionHandler(int index) { ASSERT(m_rareData); return m_rareData->m_exceptionHandlers[index]; }
434
435         void addExpressionInfo(const ExpressionRangeInfo& expressionInfo)
436         {
437             createRareDataIfNecessary();
438             m_rareData->m_expressionInfo.append(expressionInfo);
439         }
440
441         void addLineInfo(unsigned bytecodeOffset, int lineNo)
442         {
443             createRareDataIfNecessary();
444             Vector<LineInfo>& lineInfo = m_rareData->m_lineInfo;
445             if (!lineInfo.size() || lineInfo.last().lineNumber != lineNo) {
446                 LineInfo info = { bytecodeOffset, lineNo };
447                 lineInfo.append(info);
448             }
449         }
450
451         bool hasExpressionInfo() { return m_rareData && m_rareData->m_expressionInfo.size(); }
452         bool hasLineInfo() { return m_rareData && m_rareData->m_lineInfo.size(); }
453         bool needsCallReturnIndices()
454         {
455             return m_rareData &&
456                 (m_rareData->m_expressionInfo.size() || m_rareData->m_lineInfo.size() || m_rareData->m_exceptionHandlers.size());
457         }
458
459 #if ENABLE(JIT)
460         Vector<CallReturnOffsetToBytecodeOffset>& callReturnIndexVector()
461         {
462             createRareDataIfNecessary();
463             return m_rareData->m_callReturnIndexVector;
464         }
465 #endif
466
467         // Constant Pool
468
469         size_t numberOfIdentifiers() const { return m_identifiers.size(); }
470         void addIdentifier(const Identifier& i) { return m_identifiers.append(i); }
471         Identifier& identifier(int index) { return m_identifiers[index]; }
472
473         size_t numberOfConstantRegisters() const { return m_constantRegisters.size(); }
474         void addConstantRegister(const Register& r) { return m_constantRegisters.append(r); }
475         Register& constantRegister(int index) { return m_constantRegisters[index - FirstConstantRegisterIndex]; }
476         ALWAYS_INLINE bool isConstantRegisterIndex(int index) const { return index >= FirstConstantRegisterIndex; }
477         ALWAYS_INLINE JSValue getConstant(int index) const { return m_constantRegisters[index - FirstConstantRegisterIndex].jsValue(); }
478
479         unsigned addFunctionDecl(NonNullPassRefPtr<FunctionExecutable> n) { unsigned size = m_functionDecls.size(); m_functionDecls.append(n); return size; }
480         FunctionExecutable* functionDecl(int index) { return m_functionDecls[index].get(); }
481         int numberOfFunctionDecls() { return m_functionDecls.size(); }
482         unsigned addFunctionExpr(NonNullPassRefPtr<FunctionExecutable> n) { unsigned size = m_functionExprs.size(); m_functionExprs.append(n); return size; }
483         FunctionExecutable* functionExpr(int index) { return m_functionExprs[index].get(); }
484
485         unsigned addRegExp(RegExp* r) { createRareDataIfNecessary(); unsigned size = m_rareData->m_regexps.size(); m_rareData->m_regexps.append(r); return size; }
486         RegExp* regexp(int index) const { ASSERT(m_rareData); return m_rareData->m_regexps[index].get(); }
487
488         JSGlobalObject* globalObject() { return m_globalObject; }
489
490         // Jump Tables
491
492         size_t numberOfImmediateSwitchJumpTables() const { return m_rareData ? m_rareData->m_immediateSwitchJumpTables.size() : 0; }
493         SimpleJumpTable& addImmediateSwitchJumpTable() { createRareDataIfNecessary(); m_rareData->m_immediateSwitchJumpTables.append(SimpleJumpTable()); return m_rareData->m_immediateSwitchJumpTables.last(); }
494         SimpleJumpTable& immediateSwitchJumpTable(int tableIndex) { ASSERT(m_rareData); return m_rareData->m_immediateSwitchJumpTables[tableIndex]; }
495
496         size_t numberOfCharacterSwitchJumpTables() const { return m_rareData ? m_rareData->m_characterSwitchJumpTables.size() : 0; }
497         SimpleJumpTable& addCharacterSwitchJumpTable() { createRareDataIfNecessary(); m_rareData->m_characterSwitchJumpTables.append(SimpleJumpTable()); return m_rareData->m_characterSwitchJumpTables.last(); }
498         SimpleJumpTable& characterSwitchJumpTable(int tableIndex) { ASSERT(m_rareData); return m_rareData->m_characterSwitchJumpTables[tableIndex]; }
499
500         size_t numberOfStringSwitchJumpTables() const { return m_rareData ? m_rareData->m_stringSwitchJumpTables.size() : 0; }
501         StringJumpTable& addStringSwitchJumpTable() { createRareDataIfNecessary(); m_rareData->m_stringSwitchJumpTables.append(StringJumpTable()); return m_rareData->m_stringSwitchJumpTables.last(); }
502         StringJumpTable& stringSwitchJumpTable(int tableIndex) { ASSERT(m_rareData); return m_rareData->m_stringSwitchJumpTables[tableIndex]; }
503
504
505         SymbolTable* symbolTable() { return m_symbolTable; }
506         SharedSymbolTable* sharedSymbolTable() { ASSERT(m_codeType == FunctionCode); return static_cast<SharedSymbolTable*>(m_symbolTable); }
507
508         EvalCodeCache& evalCodeCache() { createRareDataIfNecessary(); return m_rareData->m_evalCodeCache; }
509
510         void shrinkToFit();
511
512         // FIXME: Make these remaining members private.
513
514         int m_numCalleeRegisters;
515         int m_numVars;
516         int m_numCapturedVars;
517         int m_numParameters;
518         bool m_isConstructor;
519
520     private:
521 #if !defined(NDEBUG) || ENABLE(OPCODE_SAMPLING)
522         void dump(ExecState*, const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator&) const;
523
524         CString registerName(ExecState*, int r) const;
525         void printUnaryOp(ExecState*, int location, Vector<Instruction>::const_iterator&, const char* op) const;
526         void printBinaryOp(ExecState*, int location, Vector<Instruction>::const_iterator&, const char* op) const;
527         void printConditionalJump(ExecState*, const Vector<Instruction>::const_iterator&, Vector<Instruction>::const_iterator&, int location, const char* op) const;
528         void printGetByIdOp(ExecState*, int location, Vector<Instruction>::const_iterator&, const char* op) const;
529         void printPutByIdOp(ExecState*, int location, Vector<Instruction>::const_iterator&, const char* op) const;
530 #endif
531
532         void createRareDataIfNecessary()
533         {
534             if (!m_rareData)
535                 m_rareData = adoptPtr(new RareData);
536         }
537
538         ScriptExecutable* m_ownerExecutable;
539         JSGlobalData* m_globalData;
540
541         Vector<Instruction> m_instructions;
542 #ifndef NDEBUG
543         unsigned m_instructionCount;
544 #endif
545
546         int m_thisRegister;
547         int m_argumentsRegister;
548         int m_activationRegister;
549
550         bool m_needsFullScopeChain;
551         bool m_usesEval;
552         bool m_isNumericCompareFunction;
553         bool m_isStrictMode;
554
555         CodeType m_codeType;
556
557         RefPtr<SourceProvider> m_source;
558         unsigned m_sourceOffset;
559
560 #if ENABLE(INTERPRETER)
561         Vector<unsigned> m_propertyAccessInstructions;
562         Vector<unsigned> m_globalResolveInstructions;
563 #endif
564 #if ENABLE(JIT)
565         Vector<StructureStubInfo> m_structureStubInfos;
566         Vector<GlobalResolveInfo> m_globalResolveInfos;
567         Vector<CallLinkInfo> m_callLinkInfos;
568         Vector<MethodCallLinkInfo> m_methodCallLinkInfos;
569         Vector<CallLinkInfo*> m_linkedCallerList;
570 #endif
571
572         Vector<unsigned> m_jumpTargets;
573
574         // Constant Pool
575         Vector<Identifier> m_identifiers;
576         Vector<Register> m_constantRegisters;
577         Vector<RefPtr<FunctionExecutable> > m_functionDecls;
578         Vector<RefPtr<FunctionExecutable> > m_functionExprs;
579
580         SymbolTable* m_symbolTable;
581
582         struct RareData {
583            WTF_MAKE_FAST_ALLOCATED;
584         public:
585             Vector<HandlerInfo> m_exceptionHandlers;
586
587             // Rare Constants
588             Vector<RefPtr<RegExp> > m_regexps;
589
590             // Jump Tables
591             Vector<SimpleJumpTable> m_immediateSwitchJumpTables;
592             Vector<SimpleJumpTable> m_characterSwitchJumpTables;
593             Vector<StringJumpTable> m_stringSwitchJumpTables;
594
595             EvalCodeCache m_evalCodeCache;
596
597             // Expression info - present if debugging.
598             Vector<ExpressionRangeInfo> m_expressionInfo;
599             // Line info - present if profiling or debugging.
600             Vector<LineInfo> m_lineInfo;
601 #if ENABLE(JIT)
602             Vector<CallReturnOffsetToBytecodeOffset> m_callReturnIndexVector;
603 #endif
604         };
605 #if PLATFORM(WIN)
606         friend void WTF::deleteOwnedPtr<RareData>(RareData*);
607 #endif
608         OwnPtr<RareData> m_rareData;
609     };
610
611     // Program code is not marked by any function, so we make the global object
612     // responsible for marking it.
613
614     class GlobalCodeBlock : public CodeBlock {
615     public:
616         GlobalCodeBlock(ScriptExecutable* ownerExecutable, CodeType codeType, JSGlobalObject* globalObject, PassRefPtr<SourceProvider> sourceProvider, unsigned sourceOffset)
617             : CodeBlock(ownerExecutable, codeType, globalObject, sourceProvider, sourceOffset, &m_unsharedSymbolTable, false)
618         {
619             m_globalObject->codeBlocks().add(this);
620         }
621
622         ~GlobalCodeBlock()
623         {
624             if (m_globalObject)
625                 m_globalObject->codeBlocks().remove(this);
626         }
627
628         void clearGlobalObject() { m_globalObject = 0; }
629
630     private:
631         SymbolTable m_unsharedSymbolTable;
632     };
633
634     class ProgramCodeBlock : public GlobalCodeBlock {
635     public:
636         ProgramCodeBlock(ProgramExecutable* ownerExecutable, CodeType codeType, JSGlobalObject* globalObject, PassRefPtr<SourceProvider> sourceProvider)
637             : GlobalCodeBlock(ownerExecutable, codeType, globalObject, sourceProvider, 0)
638         {
639         }
640     };
641
642     class EvalCodeBlock : public GlobalCodeBlock {
643     public:
644         EvalCodeBlock(EvalExecutable* ownerExecutable, JSGlobalObject* globalObject, PassRefPtr<SourceProvider> sourceProvider, int baseScopeDepth)
645             : GlobalCodeBlock(ownerExecutable, EvalCode, globalObject, sourceProvider, 0)
646             , m_baseScopeDepth(baseScopeDepth)
647         {
648         }
649
650         int baseScopeDepth() const { return m_baseScopeDepth; }
651
652         const Identifier& variable(unsigned index) { return m_variables[index]; }
653         unsigned numVariables() { return m_variables.size(); }
654         void adoptVariables(Vector<Identifier>& variables)
655         {
656             ASSERT(m_variables.isEmpty());
657             m_variables.swap(variables);
658         }
659
660     private:
661         int m_baseScopeDepth;
662         Vector<Identifier> m_variables;
663     };
664
665     class FunctionCodeBlock : public CodeBlock {
666     public:
667         // Rather than using the usual RefCounted::create idiom for SharedSymbolTable we just use new
668         // as we need to initialise the CodeBlock before we could initialise any RefPtr to hold the shared
669         // symbol table, so we just pass as a raw pointer with a ref count of 1.  We then manually deref
670         // in the destructor.
671         FunctionCodeBlock(FunctionExecutable* ownerExecutable, CodeType codeType, JSGlobalObject* globalObject, PassRefPtr<SourceProvider> sourceProvider, unsigned sourceOffset, bool isConstructor)
672             : CodeBlock(ownerExecutable, codeType, globalObject, sourceProvider, sourceOffset, SharedSymbolTable::create().leakRef(), isConstructor)
673         {
674         }
675         ~FunctionCodeBlock()
676         {
677             sharedSymbolTable()->deref();
678         }
679     };
680
681     inline Register& ExecState::r(int index)
682     {
683         CodeBlock* codeBlock = this->codeBlock();
684         if (codeBlock->isConstantRegisterIndex(index))
685             return codeBlock->constantRegister(index);
686         return this[index];
687     }
688
689     inline Register& ExecState::uncheckedR(int index)
690     {
691         ASSERT(index < FirstConstantRegisterIndex);
692         return this[index];
693     }
694     
695 } // namespace JSC
696
697 #endif // CodeBlock_h