1 | /*
|
---|
2 | * Copyright (C) 2009 Apple Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
7 | * 1. Redistributions of source code must retain the above copyright
|
---|
8 | * notice, this list of conditions and the following disclaimer.
|
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer in the
|
---|
11 | * documentation and/or other materials provided with the distribution.
|
---|
12 | *
|
---|
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
|
---|
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
|
---|
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "config.h"
|
---|
27 | #include "Executable.h"
|
---|
28 |
|
---|
29 | #include "BytecodeGenerator.h"
|
---|
30 | #include "CodeBlock.h"
|
---|
31 | #include "JIT.h"
|
---|
32 | #include "Parser.h"
|
---|
33 | #include "Vector.h"
|
---|
34 |
|
---|
35 | namespace JSC {
|
---|
36 |
|
---|
37 | #if ENABLE(JIT)
|
---|
38 | NativeExecutable::~NativeExecutable()
|
---|
39 | {
|
---|
40 | }
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | VPtrHackExecutable::~VPtrHackExecutable()
|
---|
44 | {
|
---|
45 | }
|
---|
46 |
|
---|
47 | EvalExecutable::~EvalExecutable()
|
---|
48 | {
|
---|
49 | delete m_evalCodeBlock;
|
---|
50 | }
|
---|
51 |
|
---|
52 | ProgramExecutable::~ProgramExecutable()
|
---|
53 | {
|
---|
54 | delete m_programCodeBlock;
|
---|
55 | }
|
---|
56 |
|
---|
57 | FunctionExecutable::~FunctionExecutable()
|
---|
58 | {
|
---|
59 | delete m_codeBlock;
|
---|
60 | }
|
---|
61 |
|
---|
62 | JSObject* EvalExecutable::compile(ExecState* exec, ScopeChainNode* scopeChainNode)
|
---|
63 | {
|
---|
64 | int errLine;
|
---|
65 | UString errMsg;
|
---|
66 | RefPtr<EvalNode> evalNode = exec->globalData().parser->parse<EvalNode>(&exec->globalData(), exec->lexicalGlobalObject()->debugger(), exec, m_source, &errLine, &errMsg);
|
---|
67 | if (!evalNode)
|
---|
68 | return Error::create(exec, SyntaxError, errMsg, errLine, m_source.provider()->asID(), m_source.provider()->url());
|
---|
69 | recordParse(evalNode->features(), evalNode->lineNo(), evalNode->lastLine());
|
---|
70 |
|
---|
71 | ScopeChain scopeChain(scopeChainNode);
|
---|
72 | JSGlobalObject* globalObject = scopeChain.globalObject();
|
---|
73 |
|
---|
74 | ASSERT(!m_evalCodeBlock);
|
---|
75 | m_evalCodeBlock = new EvalCodeBlock(this, globalObject, source().provider(), scopeChain.localDepth());
|
---|
76 | OwnPtr<BytecodeGenerator> generator(new BytecodeGenerator(evalNode.get(), globalObject->debugger(), scopeChain, m_evalCodeBlock->symbolTable(), m_evalCodeBlock));
|
---|
77 | generator->generate();
|
---|
78 |
|
---|
79 | evalNode->destroyData();
|
---|
80 | return 0;
|
---|
81 | }
|
---|
82 |
|
---|
83 | JSObject* ProgramExecutable::checkSyntax(ExecState* exec)
|
---|
84 | {
|
---|
85 | int errLine;
|
---|
86 | UString errMsg;
|
---|
87 | RefPtr<ProgramNode> programNode = exec->globalData().parser->parse<ProgramNode>(&exec->globalData(), exec->lexicalGlobalObject()->debugger(), exec, m_source, &errLine, &errMsg);
|
---|
88 | if (!programNode)
|
---|
89 | return Error::create(exec, SyntaxError, errMsg, errLine, m_source.provider()->asID(), m_source.provider()->url());
|
---|
90 | return 0;
|
---|
91 | }
|
---|
92 |
|
---|
93 | JSObject* ProgramExecutable::compile(ExecState* exec, ScopeChainNode* scopeChainNode)
|
---|
94 | {
|
---|
95 | int errLine;
|
---|
96 | UString errMsg;
|
---|
97 | RefPtr<ProgramNode> programNode = exec->globalData().parser->parse<ProgramNode>(&exec->globalData(), exec->lexicalGlobalObject()->debugger(), exec, m_source, &errLine, &errMsg);
|
---|
98 | if (!programNode)
|
---|
99 | return Error::create(exec, SyntaxError, errMsg, errLine, m_source.provider()->asID(), m_source.provider()->url());
|
---|
100 | recordParse(programNode->features(), programNode->lineNo(), programNode->lastLine());
|
---|
101 |
|
---|
102 | ScopeChain scopeChain(scopeChainNode);
|
---|
103 | JSGlobalObject* globalObject = scopeChain.globalObject();
|
---|
104 |
|
---|
105 | ASSERT(!m_programCodeBlock);
|
---|
106 | m_programCodeBlock = new ProgramCodeBlock(this, GlobalCode, globalObject, source().provider());
|
---|
107 | OwnPtr<BytecodeGenerator> generator(new BytecodeGenerator(programNode.get(), globalObject->debugger(), scopeChain, &globalObject->symbolTable(), m_programCodeBlock));
|
---|
108 | generator->generate();
|
---|
109 |
|
---|
110 | programNode->destroyData();
|
---|
111 | return 0;
|
---|
112 | }
|
---|
113 |
|
---|
114 | void FunctionExecutable::compile(ExecState*, ScopeChainNode* scopeChainNode)
|
---|
115 | {
|
---|
116 | JSGlobalData* globalData = scopeChainNode->globalData;
|
---|
117 | RefPtr<FunctionBodyNode> body = globalData->parser->parse<FunctionBodyNode>(globalData, 0, 0, m_source);
|
---|
118 | if (m_forceUsesArguments)
|
---|
119 | body->setUsesArguments();
|
---|
120 | body->finishParsing(m_parameters, m_name);
|
---|
121 | recordParse(body->features(), body->lineNo(), body->lastLine());
|
---|
122 |
|
---|
123 | ScopeChain scopeChain(scopeChainNode);
|
---|
124 | JSGlobalObject* globalObject = scopeChain.globalObject();
|
---|
125 |
|
---|
126 | ASSERT(!m_codeBlock);
|
---|
127 | m_codeBlock = new FunctionCodeBlock(this, FunctionCode, source().provider(), source().startOffset());
|
---|
128 | OwnPtr<BytecodeGenerator> generator(new BytecodeGenerator(body.get(), globalObject->debugger(), scopeChain, m_codeBlock->symbolTable(), m_codeBlock));
|
---|
129 | generator->generate();
|
---|
130 | m_numParameters = m_codeBlock->m_numParameters;
|
---|
131 | ASSERT(m_numParameters);
|
---|
132 | m_numVariables = m_codeBlock->m_numVars;
|
---|
133 |
|
---|
134 | body->destroyData();
|
---|
135 | }
|
---|
136 |
|
---|
137 | #if ENABLE(JIT)
|
---|
138 |
|
---|
139 | void EvalExecutable::generateJITCode(ExecState* exec, ScopeChainNode* scopeChainNode)
|
---|
140 | {
|
---|
141 | CodeBlock* codeBlock = &bytecode(exec, scopeChainNode);
|
---|
142 | m_jitCode = JIT::compile(scopeChainNode->globalData, codeBlock);
|
---|
143 |
|
---|
144 | #if !ENABLE(OPCODE_SAMPLING)
|
---|
145 | if (!BytecodeGenerator::dumpsGeneratedCode())
|
---|
146 | codeBlock->discardBytecode();
|
---|
147 | #endif
|
---|
148 | }
|
---|
149 |
|
---|
150 | void ProgramExecutable::generateJITCode(ExecState* exec, ScopeChainNode* scopeChainNode)
|
---|
151 | {
|
---|
152 | CodeBlock* codeBlock = &bytecode(exec, scopeChainNode);
|
---|
153 | m_jitCode = JIT::compile(scopeChainNode->globalData, codeBlock);
|
---|
154 |
|
---|
155 | #if !ENABLE(OPCODE_SAMPLING)
|
---|
156 | if (!BytecodeGenerator::dumpsGeneratedCode())
|
---|
157 | codeBlock->discardBytecode();
|
---|
158 | #endif
|
---|
159 | }
|
---|
160 |
|
---|
161 | void FunctionExecutable::generateJITCode(ExecState* exec, ScopeChainNode* scopeChainNode)
|
---|
162 | {
|
---|
163 | CodeBlock* codeBlock = &bytecode(exec, scopeChainNode);
|
---|
164 | m_jitCode = JIT::compile(scopeChainNode->globalData, codeBlock);
|
---|
165 |
|
---|
166 | #if !ENABLE(OPCODE_SAMPLING)
|
---|
167 | if (!BytecodeGenerator::dumpsGeneratedCode())
|
---|
168 | codeBlock->discardBytecode();
|
---|
169 | #endif
|
---|
170 | }
|
---|
171 |
|
---|
172 | #endif
|
---|
173 |
|
---|
174 | void FunctionExecutable::markAggregate(MarkStack& markStack)
|
---|
175 | {
|
---|
176 | if (m_codeBlock)
|
---|
177 | m_codeBlock->markAggregate(markStack);
|
---|
178 | }
|
---|
179 |
|
---|
180 | ExceptionInfo* FunctionExecutable::reparseExceptionInfo(JSGlobalData* globalData, ScopeChainNode* scopeChainNode, CodeBlock* codeBlock)
|
---|
181 | {
|
---|
182 | RefPtr<FunctionBodyNode> newFunctionBody = globalData->parser->parse<FunctionBodyNode>(globalData, 0, 0, m_source);
|
---|
183 | if (m_forceUsesArguments)
|
---|
184 | newFunctionBody->setUsesArguments();
|
---|
185 | newFunctionBody->finishParsing(m_parameters, m_name);
|
---|
186 |
|
---|
187 | ScopeChain scopeChain(scopeChainNode);
|
---|
188 | JSGlobalObject* globalObject = scopeChain.globalObject();
|
---|
189 |
|
---|
190 | OwnPtr<CodeBlock> newCodeBlock(new FunctionCodeBlock(this, FunctionCode, source().provider(), source().startOffset()));
|
---|
191 | globalData->functionCodeBlockBeingReparsed = newCodeBlock.get();
|
---|
192 |
|
---|
193 | OwnPtr<BytecodeGenerator> generator(new BytecodeGenerator(newFunctionBody.get(), globalObject->debugger(), scopeChain, newCodeBlock->symbolTable(), newCodeBlock.get()));
|
---|
194 | generator->setRegeneratingForExceptionInfo(static_cast<FunctionCodeBlock*>(codeBlock));
|
---|
195 | generator->generate();
|
---|
196 |
|
---|
197 | ASSERT(newCodeBlock->instructionCount() == codeBlock->instructionCount());
|
---|
198 |
|
---|
199 | #if ENABLE(JIT)
|
---|
200 | JITCode newJITCode = JIT::compile(globalData, newCodeBlock.get());
|
---|
201 | ASSERT(newJITCode.size() == generatedJITCode().size());
|
---|
202 | #endif
|
---|
203 |
|
---|
204 | globalData->functionCodeBlockBeingReparsed = 0;
|
---|
205 |
|
---|
206 | return newCodeBlock->extractExceptionInfo();
|
---|
207 | }
|
---|
208 |
|
---|
209 | ExceptionInfo* EvalExecutable::reparseExceptionInfo(JSGlobalData* globalData, ScopeChainNode* scopeChainNode, CodeBlock* codeBlock)
|
---|
210 | {
|
---|
211 | RefPtr<EvalNode> newEvalBody = globalData->parser->parse<EvalNode>(globalData, 0, 0, m_source);
|
---|
212 |
|
---|
213 | ScopeChain scopeChain(scopeChainNode);
|
---|
214 | JSGlobalObject* globalObject = scopeChain.globalObject();
|
---|
215 |
|
---|
216 | OwnPtr<EvalCodeBlock> newCodeBlock(new EvalCodeBlock(this, globalObject, source().provider(), scopeChain.localDepth()));
|
---|
217 |
|
---|
218 | OwnPtr<BytecodeGenerator> generator(new BytecodeGenerator(newEvalBody.get(), globalObject->debugger(), scopeChain, newCodeBlock->symbolTable(), newCodeBlock.get()));
|
---|
219 | generator->setRegeneratingForExceptionInfo(static_cast<EvalCodeBlock*>(codeBlock));
|
---|
220 | generator->generate();
|
---|
221 |
|
---|
222 | ASSERT(newCodeBlock->instructionCount() == codeBlock->instructionCount());
|
---|
223 |
|
---|
224 | #if ENABLE(JIT)
|
---|
225 | JITCode newJITCode = JIT::compile(globalData, newCodeBlock.get());
|
---|
226 | ASSERT(newJITCode.size() == generatedJITCode().size());
|
---|
227 | #endif
|
---|
228 |
|
---|
229 | return newCodeBlock->extractExceptionInfo();
|
---|
230 | }
|
---|
231 |
|
---|
232 | void FunctionExecutable::recompile(ExecState*)
|
---|
233 | {
|
---|
234 | delete m_codeBlock;
|
---|
235 | m_codeBlock = 0;
|
---|
236 | m_numParameters = NUM_PARAMETERS_NOT_COMPILED;
|
---|
237 | #if ENABLE(JIT)
|
---|
238 | m_jitCode = JITCode();
|
---|
239 | #endif
|
---|
240 | }
|
---|
241 |
|
---|
242 | PassRefPtr<FunctionExecutable> FunctionExecutable::fromGlobalCode(const Identifier& functionName, ExecState* exec, Debugger* debugger, const SourceCode& source, int* errLine, UString* errMsg)
|
---|
243 | {
|
---|
244 | RefPtr<ProgramNode> program = exec->globalData().parser->parse<ProgramNode>(&exec->globalData(), debugger, exec, source, errLine, errMsg);
|
---|
245 | if (!program)
|
---|
246 | return 0;
|
---|
247 |
|
---|
248 | StatementNode* exprStatement = program->singleStatement();
|
---|
249 | ASSERT(exprStatement);
|
---|
250 | ASSERT(exprStatement->isExprStatement());
|
---|
251 | if (!exprStatement || !exprStatement->isExprStatement())
|
---|
252 | return 0;
|
---|
253 |
|
---|
254 | ExpressionNode* funcExpr = static_cast<ExprStatementNode*>(exprStatement)->expr();
|
---|
255 | ASSERT(funcExpr);
|
---|
256 | ASSERT(funcExpr->isFuncExprNode());
|
---|
257 | if (!funcExpr || !funcExpr->isFuncExprNode())
|
---|
258 | return 0;
|
---|
259 |
|
---|
260 | FunctionBodyNode* body = static_cast<FuncExprNode*>(funcExpr)->body();
|
---|
261 | ASSERT(body);
|
---|
262 | return FunctionExecutable::create(&exec->globalData(), functionName, body->source(), body->usesArguments(), body->parameters(), body->lineNo(), body->lastLine());
|
---|
263 | }
|
---|
264 |
|
---|
265 | UString FunctionExecutable::paramString() const
|
---|
266 | {
|
---|
267 | FunctionParameters& parameters = *m_parameters;
|
---|
268 | UString s("");
|
---|
269 | for (size_t pos = 0; pos < parameters.size(); ++pos) {
|
---|
270 | if (!s.isEmpty())
|
---|
271 | s += ", ";
|
---|
272 | s += parameters[pos].ustring();
|
---|
273 | }
|
---|
274 |
|
---|
275 | return s;
|
---|
276 | }
|
---|
277 |
|
---|
278 | };
|
---|
279 |
|
---|
280 |
|
---|