1 | /*
|
---|
2 | * Copyright (C) 2008 Apple Inc. All rights reserved.
|
---|
3 | * Copyright (C) 2008 Cameron Zwarich <[email protected]>
|
---|
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 "Instruction.h"
|
---|
34 | #include "JSGlobalObject.h"
|
---|
35 | #include "nodes.h"
|
---|
36 | #include "SourceRange.h"
|
---|
37 | #include "ustring.h"
|
---|
38 | #include <wtf/RefPtr.h>
|
---|
39 | #include <wtf/Vector.h>
|
---|
40 |
|
---|
41 | namespace KJS {
|
---|
42 |
|
---|
43 | class ExecState;
|
---|
44 |
|
---|
45 | static ALWAYS_INLINE int missingThisObjectMarker() { return std::numeric_limits<int>::max(); }
|
---|
46 |
|
---|
47 | struct HandlerInfo {
|
---|
48 | uint32_t start;
|
---|
49 | uint32_t end;
|
---|
50 | uint32_t target;
|
---|
51 | uint32_t scopeDepth;
|
---|
52 | };
|
---|
53 |
|
---|
54 | struct ExpressionRangeInfo {
|
---|
55 | enum { MaxOffset = (1 << 7) - 1,
|
---|
56 | MaxDivot = (1 << 25) - 1
|
---|
57 | };
|
---|
58 | uint32_t instructionOffset : 25;
|
---|
59 | uint32_t divotPoint : 25;
|
---|
60 | uint32_t startOffset : 7;
|
---|
61 | uint32_t endOffset : 7;
|
---|
62 | };
|
---|
63 |
|
---|
64 | struct LineInfo {
|
---|
65 | uint32_t instructionOffset;
|
---|
66 | int32_t lineNumber;
|
---|
67 | };
|
---|
68 |
|
---|
69 | typedef HashMap<RefPtr<UString::Rep>, int32_t> StringJumpTable;
|
---|
70 | struct SimpleJumpTable {
|
---|
71 | Vector<int32_t> branchOffsets;
|
---|
72 | int32_t min;
|
---|
73 | int32_t offsetForValue(int32_t value, int32_t defaultOffset);
|
---|
74 | void add(int32_t key, int32_t offset) {
|
---|
75 | if (!branchOffsets[key])
|
---|
76 | branchOffsets[key] = offset;
|
---|
77 | }
|
---|
78 | };
|
---|
79 |
|
---|
80 | struct CodeBlock {
|
---|
81 | CodeBlock(ScopeNode* ownerNode_, CodeType codeType_, PassRefPtr<SourceProvider> source_, unsigned sourceOffset_)
|
---|
82 | : ownerNode(ownerNode_)
|
---|
83 | , globalData(0)
|
---|
84 | , numTemporaries(0)
|
---|
85 | , numVars(0)
|
---|
86 | , numParameters(0)
|
---|
87 | , numLocals(0)
|
---|
88 | , needsFullScopeChain(ownerNode_->usesEval() || ownerNode_->needsClosure())
|
---|
89 | , usesEval(ownerNode_->usesEval())
|
---|
90 | , codeType(codeType_)
|
---|
91 | , source(source_)
|
---|
92 | , sourceOffset(sourceOffset_)
|
---|
93 | {
|
---|
94 | }
|
---|
95 |
|
---|
96 | ~CodeBlock();
|
---|
97 |
|
---|
98 | #if !defined(NDEBUG) || ENABLE(SAMPLING_TOOL)
|
---|
99 | void dump(ExecState*) const;
|
---|
100 | void printStructureIDs(const Instruction*) const;
|
---|
101 | void printStructureID(const char* name, const Instruction*, int operand) const;
|
---|
102 | #endif
|
---|
103 | int expressionRangeForVPC(const Instruction*, int& divot, int& startOffset, int& endOffset);
|
---|
104 | int lineNumberForVPC(const Instruction* vPC);
|
---|
105 | bool getHandlerForVPC(const Instruction* vPC, Instruction*& target, int& scopeDepth);
|
---|
106 |
|
---|
107 | void mark();
|
---|
108 | void refStructureIDs(Instruction* vPC) const;
|
---|
109 | void derefStructureIDs(Instruction* vPC) const;
|
---|
110 |
|
---|
111 | ScopeNode* ownerNode;
|
---|
112 | JSGlobalData* globalData;
|
---|
113 |
|
---|
114 | int numConstants;
|
---|
115 | int numTemporaries;
|
---|
116 | int numVars;
|
---|
117 | int numParameters;
|
---|
118 | int numLocals;
|
---|
119 | int thisRegister;
|
---|
120 | bool needsFullScopeChain;
|
---|
121 | bool usesEval;
|
---|
122 | CodeType codeType;
|
---|
123 | RefPtr<SourceProvider> source;
|
---|
124 | unsigned sourceOffset;
|
---|
125 |
|
---|
126 | Vector<Instruction> instructions;
|
---|
127 | Vector<size_t> structureIDInstructions;
|
---|
128 |
|
---|
129 | // Constant pool
|
---|
130 | Vector<Identifier> identifiers;
|
---|
131 | Vector<RefPtr<FuncDeclNode> > functions;
|
---|
132 | Vector<RefPtr<FuncExprNode> > functionExpressions;
|
---|
133 | Vector<Register> constantRegisters;
|
---|
134 | Vector<JSValue*> unexpectedConstants;
|
---|
135 | Vector<RefPtr<RegExp> > regexps;
|
---|
136 | Vector<HandlerInfo> exceptionHandlers;
|
---|
137 | Vector<ExpressionRangeInfo> expressionInfo;
|
---|
138 | Vector<LineInfo> lineInfo;
|
---|
139 |
|
---|
140 | Vector<SimpleJumpTable> immediateSwitchJumpTables;
|
---|
141 | Vector<SimpleJumpTable> characterSwitchJumpTables;
|
---|
142 | Vector<StringJumpTable> stringSwitchJumpTables;
|
---|
143 |
|
---|
144 | private:
|
---|
145 | #if !defined(NDEBUG) || ENABLE(SAMPLING_TOOL)
|
---|
146 | void dump(ExecState*, const Vector<Instruction>::const_iterator& begin, Vector<Instruction>::const_iterator&) const;
|
---|
147 | #endif
|
---|
148 | };
|
---|
149 |
|
---|
150 | // Program code is not marked by any function, so we make the global object
|
---|
151 | // responsible for marking it.
|
---|
152 |
|
---|
153 | struct ProgramCodeBlock : public CodeBlock {
|
---|
154 | ProgramCodeBlock(ScopeNode* ownerNode_, CodeType codeType_, JSGlobalObject* globalObject_, PassRefPtr<SourceProvider> source_)
|
---|
155 | : CodeBlock(ownerNode_, codeType_, source_, 0)
|
---|
156 | , globalObject(globalObject_)
|
---|
157 | {
|
---|
158 | globalObject->codeBlocks().add(this);
|
---|
159 | }
|
---|
160 |
|
---|
161 | ~ProgramCodeBlock()
|
---|
162 | {
|
---|
163 | if (globalObject)
|
---|
164 | globalObject->codeBlocks().remove(this);
|
---|
165 | }
|
---|
166 |
|
---|
167 | JSGlobalObject* globalObject; // For program and eval nodes, the global object that marks the constant pool.
|
---|
168 | };
|
---|
169 |
|
---|
170 | struct EvalCodeBlock : public ProgramCodeBlock {
|
---|
171 | EvalCodeBlock(ScopeNode* ownerNode_, JSGlobalObject* globalObject_, PassRefPtr<SourceProvider> source_)
|
---|
172 | : ProgramCodeBlock(ownerNode_, EvalCode, globalObject_, source_)
|
---|
173 | {
|
---|
174 | }
|
---|
175 | };
|
---|
176 |
|
---|
177 | } // namespace KJS
|
---|
178 |
|
---|
179 | #endif // CodeBlock_h
|
---|