initial import
[vuplus_webkit] / Source / JavaScriptCore / bytecompiler / BytecodeGenerator.h
1 /*
2  * Copyright (C) 2008, 2009 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 BytecodeGenerator_h
31 #define BytecodeGenerator_h
32
33 #include "CodeBlock.h"
34 #include "HashTraits.h"
35 #include "Instruction.h"
36 #include "Label.h"
37 #include "LabelScope.h"
38 #include "Interpreter.h"
39 #include "RegisterID.h"
40 #include "SymbolTable.h"
41 #include "Debugger.h"
42 #include "Nodes.h"
43 #include <wtf/PassRefPtr.h>
44 #include <wtf/SegmentedVector.h>
45 #include <wtf/Vector.h>
46
47 namespace JSC {
48
49     class Identifier;
50     class ScopeChainNode;
51
52     class CallArguments {
53     public:
54         CallArguments(BytecodeGenerator& generator, ArgumentsNode* argumentsNode);
55
56         RegisterID* thisRegister() { return m_argv[0].get(); }
57         RegisterID* argumentRegister(unsigned i) { return m_argv[i + 1].get(); }
58         unsigned callFrame() { return thisRegister()->index() + count() + RegisterFile::CallFrameHeaderSize; }
59         unsigned count() { return m_argv.size(); }
60         RegisterID* profileHookRegister() { return m_profileHookRegister.get(); }
61         ArgumentsNode* argumentsNode() { return m_argumentsNode; }
62
63     private:
64         RefPtr<RegisterID> m_profileHookRegister;
65         ArgumentsNode* m_argumentsNode;
66         Vector<RefPtr<RegisterID>, 16> m_argv;
67     };
68
69     struct FinallyContext {
70         Label* finallyAddr;
71         RegisterID* retAddrDst;
72     };
73
74     struct ControlFlowContext {
75         bool isFinallyBlock;
76         FinallyContext finallyContext;
77     };
78
79     struct ForInContext {
80         RefPtr<RegisterID> expectedSubscriptRegister;
81         RefPtr<RegisterID> iterRegister;
82         RefPtr<RegisterID> indexRegister;
83         RefPtr<RegisterID> propertyRegister;
84     };
85
86     class BytecodeGenerator {
87         WTF_MAKE_FAST_ALLOCATED;
88     public:
89         typedef DeclarationStacks::VarStack VarStack;
90         typedef DeclarationStacks::FunctionStack FunctionStack;
91
92         JS_EXPORT_PRIVATE static void setDumpsGeneratedCode(bool dumpsGeneratedCode);
93         static bool dumpsGeneratedCode();
94
95         enum CompilationKind { FirstCompilation, OptimizingCompilation };
96         
97         BytecodeGenerator(ProgramNode*, ScopeChainNode*, SymbolTable*, ProgramCodeBlock*, CompilationKind);
98         BytecodeGenerator(FunctionBodyNode*, ScopeChainNode*, SymbolTable*, CodeBlock*, CompilationKind);
99         BytecodeGenerator(EvalNode*, ScopeChainNode*, SymbolTable*, EvalCodeBlock*, CompilationKind);
100
101         JSGlobalData* globalData() const { return m_globalData; }
102         const CommonIdentifiers& propertyNames() const { return *m_globalData->propertyNames; }
103
104         bool isConstructor() { return m_codeBlock->m_isConstructor; }
105
106         JSObject* generate();
107
108         // Returns the register corresponding to a local variable, or 0 if no
109         // such register exists. Registers returned by registerFor do not
110         // require explicit reference counting.
111         RegisterID* registerFor(const Identifier&);
112
113         // Returns the agument number if this is an argument, or 0 if not.
114         int argumentNumberFor(const Identifier&);
115
116         void setIsNumericCompareFunction(bool isNumericCompareFunction);
117
118         bool willResolveToArguments(const Identifier&);
119         RegisterID* uncheckedRegisterForArguments();
120
121         // Behaves as registerFor does, but ignores dynamic scope as
122         // dynamic scope should not interfere with const initialisation
123         RegisterID* constRegisterFor(const Identifier&);
124
125         // Searches the scope chain in an attempt to  statically locate the requested
126         // property.  Returns false if for any reason the property cannot be safely
127         // optimised at all.  Otherwise it will return the index and depth of the
128         // VariableObject that defines the property.  If the property cannot be found
129         // statically, depth will contain the depth of the scope chain where dynamic
130         // lookup must begin.
131         bool findScopedProperty(const Identifier&, int& index, size_t& depth, bool forWriting, bool& includesDynamicScopes, JSObject*& globalObject);
132
133         // Returns the register storing "this"
134         RegisterID* thisRegister() { return &m_thisRegister; }
135
136         bool isLocal(const Identifier&);
137         bool isLocalConstant(const Identifier&);
138
139         // Returns the next available temporary register. Registers returned by
140         // newTemporary require a modified form of reference counting: any
141         // register with a refcount of 0 is considered "available", meaning that
142         // the next instruction may overwrite it.
143         RegisterID* newTemporary();
144
145         RegisterID* highestUsedRegister();
146
147         // The same as newTemporary(), but this function returns "suggestion" if
148         // "suggestion" is a temporary. This function is helpful in situations
149         // where you've put "suggestion" in a RefPtr, but you'd like to allow
150         // the next instruction to overwrite it anyway.
151         RegisterID* newTemporaryOr(RegisterID* suggestion) { return suggestion->isTemporary() ? suggestion : newTemporary(); }
152
153         // Functions for handling of dst register
154
155         RegisterID* ignoredResult() { return &m_ignoredResultRegister; }
156
157         // Returns a place to write intermediate values of an operation
158         // which reuses dst if it is safe to do so.
159         RegisterID* tempDestination(RegisterID* dst)
160         {
161             return (dst && dst != ignoredResult() && dst->isTemporary()) ? dst : newTemporary();
162         }
163
164         // Returns the place to write the final output of an operation.
165         RegisterID* finalDestination(RegisterID* originalDst, RegisterID* tempDst = 0)
166         {
167             if (originalDst && originalDst != ignoredResult())
168                 return originalDst;
169             ASSERT(tempDst != ignoredResult());
170             if (tempDst && tempDst->isTemporary())
171                 return tempDst;
172             return newTemporary();
173         }
174
175         // Returns the place to write the final output of an operation.
176         RegisterID* finalDestinationOrIgnored(RegisterID* originalDst, RegisterID* tempDst = 0)
177         {
178             if (originalDst)
179                 return originalDst;
180             ASSERT(tempDst != ignoredResult());
181             if (tempDst && tempDst->isTemporary())
182                 return tempDst;
183             return newTemporary();
184         }
185
186         RegisterID* destinationForAssignResult(RegisterID* dst)
187         {
188             if (dst && dst != ignoredResult() && m_codeBlock->needsFullScopeChain())
189                 return dst->isTemporary() ? dst : newTemporary();
190             return 0;
191         }
192
193         // Moves src to dst if dst is not null and is different from src, otherwise just returns src.
194         RegisterID* moveToDestinationIfNeeded(RegisterID* dst, RegisterID* src)
195         {
196             return dst == ignoredResult() ? 0 : (dst && dst != src) ? emitMove(dst, src) : src;
197         }
198
199         PassRefPtr<LabelScope> newLabelScope(LabelScope::Type, const Identifier* = 0);
200         PassRefPtr<Label> newLabel();
201
202         // The emitNode functions are just syntactic sugar for calling
203         // Node::emitCode. These functions accept a 0 for the register,
204         // meaning that the node should allocate a register, or ignoredResult(),
205         // meaning that the node need not put the result in a register.
206         // Other emit functions do not accept 0 or ignoredResult().
207         RegisterID* emitNode(RegisterID* dst, Node* n)
208         {
209             // Node::emitCode assumes that dst, if provided, is either a local or a referenced temporary.
210             ASSERT(!dst || dst == ignoredResult() || !dst->isTemporary() || dst->refCount());
211             addLineInfo(n->lineNo());
212             return m_stack.recursionCheck()
213                 ? n->emitBytecode(*this, dst)
214                 : emitThrowExpressionTooDeepException();
215         }
216
217         RegisterID* emitNode(Node* n)
218         {
219             return emitNode(0, n);
220         }
221
222         void emitNodeInConditionContext(ExpressionNode* n, Label* trueTarget, Label* falseTarget, bool fallThroughMeansTrue)
223         {
224             addLineInfo(n->lineNo());
225             if (m_stack.recursionCheck())
226                 n->emitBytecodeInConditionContext(*this, trueTarget, falseTarget, fallThroughMeansTrue);
227             else
228                 emitThrowExpressionTooDeepException();
229         }
230
231         void emitExpressionInfo(unsigned divot, unsigned startOffset, unsigned endOffset)
232         {
233             if (!m_shouldEmitRichSourceInfo)
234                 return;
235
236             divot -= m_codeBlock->sourceOffset();
237             if (divot > ExpressionRangeInfo::MaxDivot) {
238                 // Overflow has occurred, we can only give line number info for errors for this region
239                 divot = 0;
240                 startOffset = 0;
241                 endOffset = 0;
242             } else if (startOffset > ExpressionRangeInfo::MaxOffset) {
243                 // If the start offset is out of bounds we clear both offsets
244                 // so we only get the divot marker.  Error message will have to be reduced
245                 // to line and column number.
246                 startOffset = 0;
247                 endOffset = 0;
248             } else if (endOffset > ExpressionRangeInfo::MaxOffset) {
249                 // The end offset is only used for additional context, and is much more likely
250                 // to overflow (eg. function call arguments) so we are willing to drop it without
251                 // dropping the rest of the range.
252                 endOffset = 0;
253             }
254             
255             ExpressionRangeInfo info;
256             info.instructionOffset = instructions().size();
257             info.divotPoint = divot;
258             info.startOffset = startOffset;
259             info.endOffset = endOffset;
260             m_codeBlock->addExpressionInfo(info);
261         }
262
263         ALWAYS_INLINE bool leftHandSideNeedsCopy(bool rightHasAssignments, bool rightIsPure)
264         {
265             return (m_codeType != FunctionCode || m_codeBlock->needsFullScopeChain() || rightHasAssignments) && !rightIsPure;
266         }
267
268         ALWAYS_INLINE PassRefPtr<RegisterID> emitNodeForLeftHandSide(ExpressionNode* n, bool rightHasAssignments, bool rightIsPure)
269         {
270             if (leftHandSideNeedsCopy(rightHasAssignments, rightIsPure)) {
271                 PassRefPtr<RegisterID> dst = newTemporary();
272                 emitNode(dst.get(), n);
273                 return dst;
274             }
275
276             return PassRefPtr<RegisterID>(emitNode(n));
277         }
278
279         RegisterID* emitLoad(RegisterID* dst, bool);
280         RegisterID* emitLoad(RegisterID* dst, double);
281         RegisterID* emitLoad(RegisterID* dst, const Identifier&);
282         RegisterID* emitLoad(RegisterID* dst, JSValue);
283
284         RegisterID* emitUnaryOp(OpcodeID, RegisterID* dst, RegisterID* src);
285         RegisterID* emitBinaryOp(OpcodeID, RegisterID* dst, RegisterID* src1, RegisterID* src2, OperandTypes);
286         RegisterID* emitEqualityOp(OpcodeID, RegisterID* dst, RegisterID* src1, RegisterID* src2);
287         RegisterID* emitUnaryNoDstOp(OpcodeID, RegisterID* src);
288
289         RegisterID* emitNewObject(RegisterID* dst);
290         RegisterID* emitNewArray(RegisterID* dst, ElementNode*, unsigned length); // stops at first elision
291
292         RegisterID* emitNewFunction(RegisterID* dst, FunctionBodyNode* body);
293         RegisterID* emitLazyNewFunction(RegisterID* dst, FunctionBodyNode* body);
294         RegisterID* emitNewFunctionInternal(RegisterID* dst, unsigned index, bool shouldNullCheck);
295         RegisterID* emitNewFunctionExpression(RegisterID* dst, FuncExprNode* func);
296         RegisterID* emitNewRegExp(RegisterID* dst, RegExp*);
297
298         RegisterID* emitMove(RegisterID* dst, RegisterID* src);
299
300         RegisterID* emitToJSNumber(RegisterID* dst, RegisterID* src) { return emitUnaryOp(op_to_jsnumber, dst, src); }
301         RegisterID* emitPreInc(RegisterID* srcDst);
302         RegisterID* emitPreDec(RegisterID* srcDst);
303         RegisterID* emitPostInc(RegisterID* dst, RegisterID* srcDst);
304         RegisterID* emitPostDec(RegisterID* dst, RegisterID* srcDst);
305
306         void emitCheckHasInstance(RegisterID* base);
307         RegisterID* emitInstanceOf(RegisterID* dst, RegisterID* value, RegisterID* base, RegisterID* basePrototype);
308         RegisterID* emitTypeOf(RegisterID* dst, RegisterID* src) { return emitUnaryOp(op_typeof, dst, src); }
309         RegisterID* emitIn(RegisterID* dst, RegisterID* property, RegisterID* base) { return emitBinaryOp(op_in, dst, property, base, OperandTypes()); }
310
311         RegisterID* emitResolve(RegisterID* dst, const Identifier& property);
312         RegisterID* emitGetScopedVar(RegisterID* dst, size_t skip, int index, JSValue globalObject);
313         RegisterID* emitPutScopedVar(size_t skip, int index, RegisterID* value, JSValue globalObject);
314
315         RegisterID* emitResolveBase(RegisterID* dst, const Identifier& property);
316         RegisterID* emitResolveBaseForPut(RegisterID* dst, const Identifier& property);
317         RegisterID* emitResolveWithBase(RegisterID* baseDst, RegisterID* propDst, const Identifier& property);
318         RegisterID* emitResolveWithThis(RegisterID* baseDst, RegisterID* propDst, const Identifier& property);
319
320         void emitMethodCheck();
321
322         RegisterID* emitGetById(RegisterID* dst, RegisterID* base, const Identifier& property);
323         RegisterID* emitGetArgumentsLength(RegisterID* dst, RegisterID* base);
324         RegisterID* emitPutById(RegisterID* base, const Identifier& property, RegisterID* value);
325         RegisterID* emitDirectPutById(RegisterID* base, const Identifier& property, RegisterID* value);
326         RegisterID* emitDeleteById(RegisterID* dst, RegisterID* base, const Identifier&);
327         RegisterID* emitGetByVal(RegisterID* dst, RegisterID* base, RegisterID* property);
328         RegisterID* emitGetArgumentByVal(RegisterID* dst, RegisterID* base, RegisterID* property);
329         RegisterID* emitPutByVal(RegisterID* base, RegisterID* property, RegisterID* value);
330         RegisterID* emitDeleteByVal(RegisterID* dst, RegisterID* base, RegisterID* property);
331         RegisterID* emitPutByIndex(RegisterID* base, unsigned index, RegisterID* value);
332         RegisterID* emitPutGetter(RegisterID* base, const Identifier& property, RegisterID* value);
333         RegisterID* emitPutSetter(RegisterID* base, const Identifier& property, RegisterID* value);
334
335         RegisterID* emitCall(RegisterID* dst, RegisterID* func, CallArguments&, unsigned divot, unsigned startOffset, unsigned endOffset);
336         RegisterID* emitCallEval(RegisterID* dst, RegisterID* func, CallArguments&, unsigned divot, unsigned startOffset, unsigned endOffset);
337         RegisterID* emitCallVarargs(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* argCount, unsigned divot, unsigned startOffset, unsigned endOffset);
338         RegisterID* emitLoadVarargs(RegisterID* argCountDst, RegisterID* thisRegister, RegisterID* args);
339
340         RegisterID* emitReturn(RegisterID* src);
341         RegisterID* emitEnd(RegisterID* src) { return emitUnaryNoDstOp(op_end, src); }
342
343         RegisterID* emitConstruct(RegisterID* dst, RegisterID* func, CallArguments&, unsigned divot, unsigned startOffset, unsigned endOffset);
344         RegisterID* emitStrcat(RegisterID* dst, RegisterID* src, int count);
345         void emitToPrimitive(RegisterID* dst, RegisterID* src);
346
347         PassRefPtr<Label> emitLabel(Label*);
348         void emitLoopHint();
349         PassRefPtr<Label> emitJump(Label* target);
350         PassRefPtr<Label> emitJumpIfTrue(RegisterID* cond, Label* target);
351         PassRefPtr<Label> emitJumpIfFalse(RegisterID* cond, Label* target);
352         PassRefPtr<Label> emitJumpIfNotFunctionCall(RegisterID* cond, Label* target);
353         PassRefPtr<Label> emitJumpIfNotFunctionApply(RegisterID* cond, Label* target);
354         PassRefPtr<Label> emitJumpScopes(Label* target, int targetScopeDepth);
355
356         PassRefPtr<Label> emitJumpSubroutine(RegisterID* retAddrDst, Label*);
357         void emitSubroutineReturn(RegisterID* retAddrSrc);
358
359         RegisterID* emitGetPropertyNames(RegisterID* dst, RegisterID* base, RegisterID* i, RegisterID* size, Label* breakTarget);
360         RegisterID* emitNextPropertyName(RegisterID* dst, RegisterID* base, RegisterID* i, RegisterID* size, RegisterID* iter, Label* target);
361
362         RegisterID* emitCatch(RegisterID*, Label* start, Label* end);
363         void emitThrow(RegisterID* exc)
364         { 
365             m_usesExceptions = true;
366             emitUnaryNoDstOp(op_throw, exc);
367         }
368
369         void emitThrowReferenceError(const UString& message);
370
371         void emitPushNewScope(RegisterID* dst, const Identifier& property, RegisterID* value);
372
373         RegisterID* emitPushScope(RegisterID* scope);
374         void emitPopScope();
375
376         void emitDebugHook(DebugHookID, int firstLine, int lastLine);
377
378         int scopeDepth() { return m_dynamicScopeDepth + m_finallyDepth; }
379         bool hasFinaliser() { return m_finallyDepth != 0; }
380
381         void pushFinallyContext(Label* target, RegisterID* returnAddrDst);
382         void popFinallyContext();
383
384         void pushOptimisedForIn(RegisterID* expectedBase, RegisterID* iter, RegisterID* index, RegisterID* propertyRegister)
385         {
386             ForInContext context = { expectedBase, iter, index, propertyRegister };
387             m_forInContextStack.append(context);
388         }
389
390         void popOptimisedForIn()
391         {
392             m_forInContextStack.removeLast();
393         }
394
395         LabelScope* breakTarget(const Identifier&);
396         LabelScope* continueTarget(const Identifier&);
397
398         void beginSwitch(RegisterID*, SwitchInfo::SwitchType);
399         void endSwitch(uint32_t clauseCount, RefPtr<Label>*, ExpressionNode**, Label* defaultLabel, int32_t min, int32_t range);
400
401         CodeType codeType() const { return m_codeType; }
402
403         bool shouldEmitProfileHooks() { return m_shouldEmitProfileHooks; }
404         
405         bool isStrictMode() const { return m_codeBlock->isStrictMode(); }
406         
407         ScopeChainNode* scopeChain() const { return m_scopeChain.get(); }
408
409     private:
410         void emitOpcode(OpcodeID);
411         void retrieveLastBinaryOp(int& dstIndex, int& src1Index, int& src2Index);
412         void retrieveLastUnaryOp(int& dstIndex, int& srcIndex);
413         ALWAYS_INLINE void rewindBinaryOp();
414         ALWAYS_INLINE void rewindUnaryOp();
415
416         PassRefPtr<Label> emitComplexJumpScopes(Label* target, ControlFlowContext* topScope, ControlFlowContext* bottomScope);
417
418         typedef HashMap<EncodedJSValue, unsigned, EncodedJSValueHash, EncodedJSValueHashTraits> JSValueMap;
419
420         struct IdentifierMapIndexHashTraits {
421             typedef int TraitType;
422             typedef IdentifierMapIndexHashTraits StorageTraits;
423             static int emptyValue() { return std::numeric_limits<int>::max(); }
424             static const bool emptyValueIsZero = false;
425             static const bool needsDestruction = false;
426             static const bool needsRef = false;
427         };
428
429         typedef HashMap<RefPtr<StringImpl>, int, IdentifierRepHash, HashTraits<RefPtr<StringImpl> >, IdentifierMapIndexHashTraits> IdentifierMap;
430         typedef HashMap<double, JSValue> NumberMap;
431         typedef HashMap<StringImpl*, JSString*, IdentifierRepHash> IdentifierStringMap;
432         
433         RegisterID* emitCall(OpcodeID, RegisterID* dst, RegisterID* func, CallArguments&, unsigned divot, unsigned startOffset, unsigned endOffset);
434
435         RegisterID* newRegister();
436
437         // Adds a var slot and maps it to the name ident in symbolTable().
438         RegisterID* addVar(const Identifier& ident, bool isConstant)
439         {
440             RegisterID* local;
441             addVar(ident, isConstant, local);
442             return local;
443         }
444
445         // Ditto. Returns true if a new RegisterID was added, false if a pre-existing RegisterID was re-used.
446         bool addVar(const Identifier&, bool isConstant, RegisterID*&);
447         
448         // Adds an anonymous var slot. To give this slot a name, add it to symbolTable().
449         RegisterID* addVar()
450         {
451             ++m_codeBlock->m_numVars;
452             return newRegister();
453         }
454
455         // Returns the index of the added var.
456         int addGlobalVar(const Identifier&, bool isConstant);
457
458         void addParameter(const Identifier&, int parameterIndex);
459         
460         void preserveLastVar();
461         bool shouldAvoidResolveGlobal();
462
463         RegisterID& registerFor(int index)
464         {
465             if (index >= 0)
466                 return m_calleeRegisters[index];
467
468             ASSERT(m_parameters.size());
469             return m_parameters[index + m_parameters.size() + RegisterFile::CallFrameHeaderSize];
470         }
471
472         unsigned addConstant(const Identifier&);
473         RegisterID* addConstantValue(JSValue);
474         unsigned addRegExp(RegExp*);
475
476         unsigned addConstantBuffer(unsigned length);
477         
478         FunctionExecutable* makeFunction(ExecState* exec, FunctionBodyNode* body)
479         {
480             return FunctionExecutable::create(exec, body->ident(), body->source(), body->usesArguments(), body->parameters(), body->isStrictMode(), body->lineNo(), body->lastLine());
481         }
482
483         FunctionExecutable* makeFunction(JSGlobalData* globalData, FunctionBodyNode* body)
484         {
485             return FunctionExecutable::create(*globalData, body->ident(), body->source(), body->usesArguments(), body->parameters(), body->isStrictMode(), body->lineNo(), body->lastLine());
486         }
487
488         JSString* addStringConstant(const Identifier&);
489
490         void addLineInfo(unsigned lineNo)
491         {
492 #if !ENABLE(OPCODE_SAMPLING)
493             if (m_shouldEmitRichSourceInfo)
494 #endif
495                 m_codeBlock->addLineInfo(instructions().size(), lineNo);
496         }
497
498         RegisterID* emitInitLazyRegister(RegisterID*);
499
500         Vector<Instruction>& instructions() { return m_codeBlock->instructions(); }
501         SymbolTable& symbolTable() { return *m_symbolTable; }
502
503         bool shouldOptimizeLocals()
504         {
505             if (m_dynamicScopeDepth)
506                 return false;
507
508             if (m_codeType != FunctionCode)
509                 return false;
510
511             return true;
512         }
513
514         bool canOptimizeNonLocals()
515         {
516             if (m_dynamicScopeDepth)
517                 return false;
518
519             if (m_codeType == EvalCode)
520                 return false;
521
522             if (m_codeType == FunctionCode && m_codeBlock->usesEval())
523                 return false;
524
525             return true;
526         }
527
528         RegisterID* emitThrowExpressionTooDeepException();
529
530         void createArgumentsIfNecessary();
531         void createActivationIfNecessary();
532         RegisterID* createLazyRegisterIfNecessary(RegisterID*);
533
534         bool m_shouldEmitDebugHooks;
535         bool m_shouldEmitProfileHooks;
536         bool m_shouldEmitRichSourceInfo;
537
538         Strong<ScopeChainNode> m_scopeChain;
539         SymbolTable* m_symbolTable;
540
541         ScopeNode* m_scopeNode;
542         CodeBlock* m_codeBlock;
543
544         // Some of these objects keep pointers to one another. They are arranged
545         // to ensure a sane destruction order that avoids references to freed memory.
546         HashSet<RefPtr<StringImpl>, IdentifierRepHash> m_functions;
547         RegisterID m_ignoredResultRegister;
548         RegisterID m_thisRegister;
549         RegisterID* m_activationRegister;
550         SegmentedVector<RegisterID, 32> m_constantPoolRegisters;
551         SegmentedVector<RegisterID, 32> m_calleeRegisters;
552         SegmentedVector<RegisterID, 32> m_parameters;
553         SegmentedVector<Label, 32> m_labels;
554         SegmentedVector<LabelScope, 8> m_labelScopes;
555         RefPtr<RegisterID> m_lastVar;
556         int m_finallyDepth;
557         int m_dynamicScopeDepth;
558         int m_baseScopeDepth;
559         CodeType m_codeType;
560
561         Vector<ControlFlowContext> m_scopeContextStack;
562         Vector<SwitchInfo> m_switchContextStack;
563         Vector<ForInContext> m_forInContextStack;
564
565         int m_firstConstantIndex;
566         int m_nextConstantOffset;
567         unsigned m_globalConstantIndex;
568
569         int m_globalVarStorageOffset;
570
571         bool m_hasCreatedActivation;
572         int m_firstLazyFunction;
573         int m_lastLazyFunction;
574         HashMap<unsigned int, FunctionBodyNode*, WTF::IntHash<unsigned int>, WTF::UnsignedWithZeroKeyHashTraits<unsigned int> > m_lazyFunctions;
575         typedef HashMap<FunctionBodyNode*, unsigned> FunctionOffsetMap;
576         FunctionOffsetMap m_functionOffsets;
577         
578         // Constant pool
579         IdentifierMap m_identifierMap;
580         JSValueMap m_jsValueMap;
581         NumberMap m_numberMap;
582         IdentifierStringMap m_stringMap;
583
584         JSGlobalData* m_globalData;
585
586         OpcodeID m_lastOpcodeID;
587 #ifndef NDEBUG
588         size_t m_lastOpcodePosition;
589 #endif
590
591         StackBounds m_stack;
592
593         bool m_usesExceptions;
594         bool m_expressionTooDeep;
595     };
596
597 }
598
599 #endif // BytecodeGenerator_h