tizen beta release
[framework/web/webkit-efl.git] / Source / JavaScriptCore / runtime / Executable.cpp
1 /*
2  * Copyright (C) 2009, 2010 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 "DFGDriver.h"
32 #include "JIT.h"
33 #include "Parser.h"
34 #include "UStringBuilder.h"
35 #include "Vector.h"
36
37 namespace JSC {
38
39 const ClassInfo ExecutableBase::s_info = { "Executable", 0, 0, 0, CREATE_METHOD_TABLE(ExecutableBase) };
40
41 inline void ExecutableBase::clearCode()
42 {
43 #if ENABLE(JIT)
44     m_jitCodeForCall.clear();
45     m_jitCodeForConstruct.clear();
46     m_jitCodeForCallWithArityCheck = MacroAssemblerCodePtr();
47     m_jitCodeForConstructWithArityCheck = MacroAssemblerCodePtr();
48 #endif
49     m_numParametersForCall = NUM_PARAMETERS_NOT_COMPILED;
50     m_numParametersForConstruct = NUM_PARAMETERS_NOT_COMPILED;
51 }
52
53 #if ENABLE(DFG_JIT)
54 DFG::Intrinsic ExecutableBase::intrinsic() const
55 {
56     if (const NativeExecutable* nativeExecutable = jsDynamicCast<const NativeExecutable*>(this))
57         return nativeExecutable->intrinsic();
58     return DFG::NoIntrinsic;
59 }
60 #endif
61
62 const ClassInfo NativeExecutable::s_info = { "NativeExecutable", &ExecutableBase::s_info, 0, 0, CREATE_METHOD_TABLE(NativeExecutable) };
63
64 NativeExecutable::~NativeExecutable()
65 {
66 }
67
68 #if ENABLE(DFG_JIT)
69 DFG::Intrinsic NativeExecutable::intrinsic() const
70 {
71     return m_intrinsic;
72 }
73 #endif
74
75 #if ENABLE(JIT)
76 // Utility method used for jettisoning code blocks.
77 template<typename T>
78 static void jettisonCodeBlock(JSGlobalData& globalData, OwnPtr<T>& codeBlock)
79 {
80     ASSERT(codeBlock->getJITType() != JITCode::BaselineJIT);
81     ASSERT(codeBlock->alternative());
82     OwnPtr<T> codeBlockToJettison = codeBlock.release();
83     codeBlock = static_pointer_cast<T>(codeBlockToJettison->releaseAlternative());
84     codeBlockToJettison->unlinkIncomingCalls();
85     globalData.heap.jettisonDFGCodeBlock(static_pointer_cast<CodeBlock>(codeBlockToJettison.release()));
86 }
87 #endif
88
89 void NativeExecutable::finalize(JSCell* cell)
90 {
91     jsCast<NativeExecutable*>(cell)->clearCode();
92 }
93
94 const ClassInfo ScriptExecutable::s_info = { "ScriptExecutable", &ExecutableBase::s_info, 0, 0, CREATE_METHOD_TABLE(ScriptExecutable) };
95
96 const ClassInfo EvalExecutable::s_info = { "EvalExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(EvalExecutable) };
97
98 EvalExecutable::EvalExecutable(ExecState* exec, const SourceCode& source, bool inStrictContext)
99     : ScriptExecutable(exec->globalData().evalExecutableStructure.get(), exec, source, inStrictContext)
100 {
101 }
102
103 EvalExecutable::~EvalExecutable()
104 {
105 }
106
107 const ClassInfo ProgramExecutable::s_info = { "ProgramExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(ProgramExecutable) };
108
109 ProgramExecutable::ProgramExecutable(ExecState* exec, const SourceCode& source)
110     : ScriptExecutable(exec->globalData().programExecutableStructure.get(), exec, source, false)
111 {
112 }
113
114 ProgramExecutable::~ProgramExecutable()
115 {
116 }
117
118 const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(FunctionExecutable) };
119
120 FunctionExecutable::FunctionExecutable(JSGlobalData& globalData, const Identifier& name, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool inStrictContext)
121     : ScriptExecutable(globalData.functionExecutableStructure.get(), globalData, source, inStrictContext)
122     , m_numCapturedVariables(0)
123     , m_forceUsesArguments(forceUsesArguments)
124     , m_parameters(parameters)
125     , m_name(name)
126     , m_symbolTable(0)
127 {
128 }
129
130 FunctionExecutable::FunctionExecutable(ExecState* exec, const Identifier& name, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool inStrictContext)
131     : ScriptExecutable(exec->globalData().functionExecutableStructure.get(), exec, source, inStrictContext)
132     , m_numCapturedVariables(0)
133     , m_forceUsesArguments(forceUsesArguments)
134     , m_parameters(parameters)
135     , m_name(name)
136     , m_symbolTable(0)
137 {
138 }
139
140 FunctionExecutable::~FunctionExecutable()
141 {
142 }
143
144 JSObject* EvalExecutable::compileOptimized(ExecState* exec, ScopeChainNode* scopeChainNode)
145 {
146     ASSERT(exec->globalData().dynamicGlobalObject);
147     ASSERT(!!m_evalCodeBlock);
148     JSObject* error = 0;
149     if (m_evalCodeBlock->getJITType() != JITCode::topTierJIT())
150         error = compileInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_evalCodeBlock->getJITType()));
151     ASSERT(!!m_evalCodeBlock);
152     return error;
153 }
154
155 JSObject* EvalExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
156 {
157     SamplingRegion samplingRegion(jitType == JITCode::BaselineJIT ? "Baseline Compilation (TOTAL)" : "DFG Compilation (TOTAL)");
158     
159 #if !ENABLE(JIT)
160     UNUSED_PARAM(jitType);
161 #endif
162     JSObject* exception = 0;
163     JSGlobalData* globalData = &exec->globalData();
164     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
165     
166     if (!!m_evalCodeBlock && m_evalCodeBlock->canProduceCopyWithBytecode()) {
167         BytecodeDestructionBlocker blocker(m_evalCodeBlock.get());
168         OwnPtr<EvalCodeBlock> newCodeBlock = adoptPtr(new EvalCodeBlock(CodeBlock::CopyParsedBlock, *m_evalCodeBlock));
169         newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_evalCodeBlock.release()));
170         m_evalCodeBlock = newCodeBlock.release();
171     } else {
172         if (!lexicalGlobalObject->evalEnabled())
173             return throwError(exec, createEvalError(exec, "Eval is disabled"));
174         RefPtr<EvalNode> evalNode = parse<EvalNode>(globalData, lexicalGlobalObject, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, EvalNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, lexicalGlobalObject->debugger(), exec, &exception);
175         if (!evalNode) {
176             ASSERT(exception);
177             return exception;
178         }
179         recordParse(evalNode->features(), evalNode->hasCapturedVariables(), evalNode->lineNo(), evalNode->lastLine());
180         
181         JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
182         
183         OwnPtr<CodeBlock> previousCodeBlock = m_evalCodeBlock.release();
184         ASSERT((jitType == JITCode::bottomTierJIT()) == !previousCodeBlock);
185         m_evalCodeBlock = adoptPtr(new EvalCodeBlock(this, globalObject, source().provider(), scopeChainNode->localDepth(), previousCodeBlock.release()));
186         OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(evalNode.get(), scopeChainNode, m_evalCodeBlock->symbolTable(), m_evalCodeBlock.get(), !!m_evalCodeBlock->alternative() ? OptimizingCompilation : FirstCompilation)));
187         if ((exception = generator->generate())) {
188             m_evalCodeBlock = static_pointer_cast<EvalCodeBlock>(m_evalCodeBlock->releaseAlternative());
189             evalNode->destroyData();
190             return exception;
191         }
192         
193         evalNode->destroyData();
194         m_evalCodeBlock->copyPostParseDataFromAlternative();
195     }
196
197 #if ENABLE(JIT)
198     if (exec->globalData().canUseJIT()) {
199         bool dfgCompiled = false;
200         if (jitType == JITCode::DFGJIT)
201             dfgCompiled = DFG::tryCompile(exec, m_evalCodeBlock.get(), m_jitCodeForCall);
202         if (dfgCompiled)
203             ASSERT(!m_evalCodeBlock->alternative() || !m_evalCodeBlock->alternative()->hasIncomingCalls());
204         else {
205             if (m_evalCodeBlock->alternative()) {
206                 // There is already an alternative piece of code compiled with a different
207                 // JIT, so we can silently fail.
208                 m_evalCodeBlock = static_pointer_cast<EvalCodeBlock>(m_evalCodeBlock->releaseAlternative());
209                 return 0;
210             }
211             m_jitCodeForCall = JIT::compile(scopeChainNode->globalData, m_evalCodeBlock.get());
212         }
213 #if !ENABLE(OPCODE_SAMPLING)
214         if (!BytecodeGenerator::dumpsGeneratedCode())
215             m_evalCodeBlock->handleBytecodeDiscardingOpportunity();
216 #endif
217         m_evalCodeBlock->setJITCode(m_jitCodeForCall, MacroAssemblerCodePtr());
218     }
219 #endif
220
221 #if ENABLE(JIT)
222 #if ENABLE(INTERPRETER)
223     if (!m_jitCodeForCall)
224         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
225     else
226 #endif
227     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock) + m_jitCodeForCall.size());
228 #else
229     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
230 #endif
231
232     return 0;
233 }
234
235 #if ENABLE(JIT)
236 void EvalExecutable::jettisonOptimizedCode(JSGlobalData& globalData)
237 {
238     jettisonCodeBlock(globalData, m_evalCodeBlock);
239     m_jitCodeForCall = m_evalCodeBlock->getJITCode();
240     ASSERT(!m_jitCodeForCallWithArityCheck);
241 }
242 #endif
243
244 void EvalExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
245 {
246     EvalExecutable* thisObject = jsCast<EvalExecutable*>(cell);
247     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
248     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
249     ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
250     ScriptExecutable::visitChildren(thisObject, visitor);
251     if (thisObject->m_evalCodeBlock)
252         thisObject->m_evalCodeBlock->visitAggregate(visitor);
253 }
254
255 void EvalExecutable::unlinkCalls()
256 {
257 #if ENABLE(JIT)
258     if (!m_jitCodeForCall)
259         return;
260     ASSERT(m_evalCodeBlock);
261     m_evalCodeBlock->unlinkCalls();
262 #endif
263 }
264
265 void EvalExecutable::finalize(JSCell* cell)
266 {
267     jsCast<EvalExecutable*>(cell)->clearCode();
268 }
269
270 inline void EvalExecutable::clearCode()
271 {
272     if (m_evalCodeBlock) {
273         m_evalCodeBlock->clearEvalCache();
274         m_evalCodeBlock.clear();
275     }
276     Base::clearCode();
277 }
278
279 JSObject* ProgramExecutable::checkSyntax(ExecState* exec)
280 {
281     JSObject* exception = 0;
282     JSGlobalData* globalData = &exec->globalData();
283     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
284     RefPtr<ProgramNode> programNode = parse<ProgramNode>(globalData, lexicalGlobalObject, m_source, 0, JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, lexicalGlobalObject->debugger(), exec, &exception);
285     if (programNode)
286         return 0;
287     ASSERT(exception);
288     return exception;
289 }
290
291 JSObject* ProgramExecutable::compileOptimized(ExecState* exec, ScopeChainNode* scopeChainNode)
292 {
293     ASSERT(exec->globalData().dynamicGlobalObject);
294     ASSERT(!!m_programCodeBlock);
295     JSObject* error = 0;
296     if (m_programCodeBlock->getJITType() != JITCode::topTierJIT())
297         error = compileInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_programCodeBlock->getJITType()));
298     ASSERT(!!m_programCodeBlock);
299     return error;
300 }
301
302 JSObject* ProgramExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
303 {
304     SamplingRegion samplingRegion(jitType == JITCode::BaselineJIT ? "Baseline Compilation (TOTAL)" : "DFG Compilation (TOTAL)");
305     
306 #if !ENABLE(JIT)
307     UNUSED_PARAM(jitType);
308 #endif
309     JSObject* exception = 0;
310     JSGlobalData* globalData = &exec->globalData();
311     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
312     
313     if (!!m_programCodeBlock && m_programCodeBlock->canProduceCopyWithBytecode()) {
314         BytecodeDestructionBlocker blocker(m_programCodeBlock.get());
315         OwnPtr<ProgramCodeBlock> newCodeBlock = adoptPtr(new ProgramCodeBlock(CodeBlock::CopyParsedBlock, *m_programCodeBlock));
316         newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_programCodeBlock.release()));
317         m_programCodeBlock = newCodeBlock.release();
318     } else {
319         RefPtr<ProgramNode> programNode = parse<ProgramNode>(globalData, lexicalGlobalObject, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, lexicalGlobalObject->debugger(), exec, &exception);
320         if (!programNode) {
321             ASSERT(exception);
322             return exception;
323         }
324         recordParse(programNode->features(), programNode->hasCapturedVariables(), programNode->lineNo(), programNode->lastLine());
325
326         JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
327     
328         OwnPtr<CodeBlock> previousCodeBlock = m_programCodeBlock.release();
329         ASSERT((jitType == JITCode::bottomTierJIT()) == !previousCodeBlock);
330         m_programCodeBlock = adoptPtr(new ProgramCodeBlock(this, GlobalCode, globalObject, source().provider(), previousCodeBlock.release()));
331         OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(programNode.get(), scopeChainNode, &globalObject->symbolTable(), m_programCodeBlock.get(), !!m_programCodeBlock->alternative() ? OptimizingCompilation : FirstCompilation)));
332         if ((exception = generator->generate())) {
333             m_programCodeBlock = static_pointer_cast<ProgramCodeBlock>(m_programCodeBlock->releaseAlternative());
334             programNode->destroyData();
335             return exception;
336         }
337
338         programNode->destroyData();
339         m_programCodeBlock->copyPostParseDataFromAlternative();
340     }
341
342 #if ENABLE(JIT)
343     if (exec->globalData().canUseJIT()) {
344         bool dfgCompiled = false;
345         if (jitType == JITCode::DFGJIT)
346             dfgCompiled = DFG::tryCompile(exec, m_programCodeBlock.get(), m_jitCodeForCall);
347         if (dfgCompiled) {
348             if (m_programCodeBlock->alternative())
349                 m_programCodeBlock->alternative()->unlinkIncomingCalls();
350         } else {
351             if (m_programCodeBlock->alternative()) {
352                 m_programCodeBlock = static_pointer_cast<ProgramCodeBlock>(m_programCodeBlock->releaseAlternative());
353                 return 0;
354             }
355             m_jitCodeForCall = JIT::compile(scopeChainNode->globalData, m_programCodeBlock.get());
356         }
357 #if !ENABLE(OPCODE_SAMPLING)
358         if (!BytecodeGenerator::dumpsGeneratedCode())
359             m_programCodeBlock->handleBytecodeDiscardingOpportunity();
360 #endif
361         m_programCodeBlock->setJITCode(m_jitCodeForCall, MacroAssemblerCodePtr());
362     }
363 #endif
364
365 #if ENABLE(JIT)
366 #if ENABLE(INTERPRETER)
367     if (!m_jitCodeForCall)
368         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
369     else
370 #endif
371         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock) + m_jitCodeForCall.size());
372 #else
373     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
374 #endif
375
376     return 0;
377 }
378
379 #if ENABLE(JIT)
380 void ProgramExecutable::jettisonOptimizedCode(JSGlobalData& globalData)
381 {
382     jettisonCodeBlock(globalData, m_programCodeBlock);
383     m_jitCodeForCall = m_programCodeBlock->getJITCode();
384     ASSERT(!m_jitCodeForCallWithArityCheck);
385 }
386 #endif
387
388 void ProgramExecutable::unlinkCalls()
389 {
390 #if ENABLE(JIT)
391     if (!m_jitCodeForCall)
392         return;
393     ASSERT(m_programCodeBlock);
394     m_programCodeBlock->unlinkCalls();
395 #endif
396 }
397
398 void ProgramExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
399 {
400     ProgramExecutable* thisObject = jsCast<ProgramExecutable*>(cell);
401     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
402     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
403     ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
404     ScriptExecutable::visitChildren(thisObject, visitor);
405     if (thisObject->m_programCodeBlock)
406         thisObject->m_programCodeBlock->visitAggregate(visitor);
407 }
408
409 void ProgramExecutable::finalize(JSCell* cell)
410 {
411     jsCast<ProgramExecutable*>(cell)->clearCode();
412 }
413
414 inline void ProgramExecutable::clearCode()
415 {
416     if (m_programCodeBlock) {
417         m_programCodeBlock->clearEvalCache();
418         m_programCodeBlock.clear();
419     }
420     Base::clearCode();
421 }
422
423 FunctionCodeBlock* FunctionExecutable::baselineCodeBlockFor(CodeSpecializationKind kind)
424 {
425     FunctionCodeBlock* result;
426     if (kind == CodeForCall)
427         result = m_codeBlockForCall.get();
428     else {
429         ASSERT(kind == CodeForConstruct);
430         result = m_codeBlockForConstruct.get();
431     }
432     if (!result)
433         return 0;
434     while (result->alternative())
435         result = static_cast<FunctionCodeBlock*>(result->alternative());
436     ASSERT(result);
437     ASSERT(result->getJITType() == JITCode::BaselineJIT);
438     return result;
439 }
440
441 JSObject* FunctionExecutable::compileOptimizedForCall(ExecState* exec, ScopeChainNode* scopeChainNode)
442 {
443     ASSERT(exec->globalData().dynamicGlobalObject);
444     ASSERT(!!m_codeBlockForCall);
445     JSObject* error = 0;
446     if (m_codeBlockForCall->getJITType() != JITCode::topTierJIT())
447         error = compileForCallInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_codeBlockForCall->getJITType()));
448     ASSERT(!!m_codeBlockForCall);
449     return error;
450 }
451
452 JSObject* FunctionExecutable::compileOptimizedForConstruct(ExecState* exec, ScopeChainNode* scopeChainNode)
453 {
454     ASSERT(exec->globalData().dynamicGlobalObject);
455     ASSERT(!!m_codeBlockForConstruct);
456     JSObject* error = 0;
457     if (m_codeBlockForConstruct->getJITType() != JITCode::topTierJIT())
458         error = compileForConstructInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_codeBlockForConstruct->getJITType()));
459     ASSERT(!!m_codeBlockForConstruct);
460     return error;
461 }
462
463 FunctionCodeBlock* FunctionExecutable::codeBlockWithBytecodeFor(CodeSpecializationKind kind)
464 {
465     FunctionCodeBlock* codeBlock = baselineCodeBlockFor(kind);
466     if (codeBlock->canProduceCopyWithBytecode())
467         return codeBlock;
468     return 0;
469 }
470
471 PassOwnPtr<FunctionCodeBlock> FunctionExecutable::produceCodeBlockFor(ScopeChainNode* scopeChainNode, CompilationKind compilationKind, CodeSpecializationKind specializationKind, JSObject*& exception)
472 {
473     if (!!codeBlockFor(specializationKind) && codeBlockFor(specializationKind)->canProduceCopyWithBytecode()) {
474         BytecodeDestructionBlocker blocker(codeBlockFor(specializationKind).get());
475         return adoptPtr(new FunctionCodeBlock(CodeBlock::CopyParsedBlock, *codeBlockFor(specializationKind)));
476     }
477     
478     exception = 0;
479     JSGlobalData* globalData = scopeChainNode->globalData;
480     JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
481     RefPtr<FunctionBodyNode> body = parse<FunctionBodyNode>(globalData, globalObject, m_source, m_parameters.get(), isStrictMode() ? JSParseStrict : JSParseNormal, FunctionBodyNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, 0, 0, &exception);
482
483     if (!body) {
484         ASSERT(exception);
485         return nullptr;
486     }
487     if (m_forceUsesArguments)
488         body->setUsesArguments();
489     body->finishParsing(m_parameters, m_name);
490     recordParse(body->features(), body->hasCapturedVariables(), body->lineNo(), body->lastLine());
491
492     OwnPtr<FunctionCodeBlock> result;
493     ASSERT((compilationKind == FirstCompilation) == !codeBlockFor(specializationKind));
494     result = adoptPtr(new FunctionCodeBlock(this, FunctionCode, globalObject, source().provider(), source().startOffset(), specializationKind == CodeForConstruct));
495     OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(body.get(), scopeChainNode, result->symbolTable(), result.get(), compilationKind)));
496     exception = generator->generate();
497     body->destroyData();
498     if (exception)
499         return nullptr;
500
501     result->copyPostParseDataFrom(codeBlockFor(specializationKind).get());
502     return result.release();
503 }
504
505 JSObject* FunctionExecutable::compileForCallInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
506 {
507     SamplingRegion samplingRegion(jitType == JITCode::BaselineJIT ? "Baseline Compilation (TOTAL)" : "DFG Compilation (TOTAL)");
508     
509 #if !ENABLE(JIT)
510     UNUSED_PARAM(exec);
511     UNUSED_PARAM(jitType);
512     UNUSED_PARAM(exec);
513 #endif
514     ASSERT((jitType == JITCode::bottomTierJIT()) == !m_codeBlockForCall);
515     JSObject* exception;
516     OwnPtr<FunctionCodeBlock> newCodeBlock = produceCodeBlockFor(scopeChainNode, !!m_codeBlockForCall ? OptimizingCompilation : FirstCompilation, CodeForCall, exception);
517     if (!newCodeBlock)
518         return exception;
519
520     newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_codeBlockForCall.release()));
521     m_codeBlockForCall = newCodeBlock.release();
522     
523     m_numParametersForCall = m_codeBlockForCall->m_numParameters;
524     ASSERT(m_numParametersForCall);
525     m_numCapturedVariables = m_codeBlockForCall->m_numCapturedVars;
526     m_symbolTable = m_codeBlockForCall->sharedSymbolTable();
527
528 #if ENABLE(JIT)
529     JSGlobalData* globalData = scopeChainNode->globalData;
530     if (globalData->canUseJIT()) {
531         bool dfgCompiled = false;
532         if (jitType == JITCode::DFGJIT)
533             dfgCompiled = DFG::tryCompileFunction(exec, m_codeBlockForCall.get(), m_jitCodeForCall, m_jitCodeForCallWithArityCheck);
534         if (dfgCompiled) {
535             if (m_codeBlockForCall->alternative())
536                 m_codeBlockForCall->alternative()->unlinkIncomingCalls();
537         } else {
538             if (m_codeBlockForCall->alternative()) {
539                 m_codeBlockForCall = static_pointer_cast<FunctionCodeBlock>(m_codeBlockForCall->releaseAlternative());
540                 m_symbolTable = m_codeBlockForCall->sharedSymbolTable();
541                 return 0;
542             }
543             m_jitCodeForCall = JIT::compile(globalData, m_codeBlockForCall.get(), &m_jitCodeForCallWithArityCheck);
544         }
545 #if !ENABLE(OPCODE_SAMPLING)
546         if (!BytecodeGenerator::dumpsGeneratedCode())
547             m_codeBlockForCall->handleBytecodeDiscardingOpportunity();
548 #endif
549         
550         m_codeBlockForCall->setJITCode(m_jitCodeForCall, m_jitCodeForCallWithArityCheck);
551     }
552 #endif
553
554 #if ENABLE(JIT)
555 #if ENABLE(INTERPRETER)
556     if (!m_jitCodeForCall)
557         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
558     else
559 #endif
560         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall) + m_jitCodeForCall.size());
561 #else
562     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
563 #endif
564
565     return 0;
566 }
567
568 JSObject* FunctionExecutable::compileForConstructInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
569 {
570     SamplingRegion samplingRegion(jitType == JITCode::BaselineJIT ? "Baseline Compilation (TOTAL)" : "DFG Compilation (TOTAL)");
571     
572 #if !ENABLE(JIT)
573     UNUSED_PARAM(jitType);
574     UNUSED_PARAM(exec);
575 #endif
576     
577     ASSERT((jitType == JITCode::bottomTierJIT()) == !m_codeBlockForConstruct);
578     JSObject* exception;
579     OwnPtr<FunctionCodeBlock> newCodeBlock = produceCodeBlockFor(scopeChainNode, !!m_codeBlockForConstruct ? OptimizingCompilation : FirstCompilation, CodeForConstruct, exception);
580     if (!newCodeBlock)
581         return exception;
582
583     newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_codeBlockForConstruct.release()));
584     m_codeBlockForConstruct = newCodeBlock.release();
585     
586     m_numParametersForConstruct = m_codeBlockForConstruct->m_numParameters;
587     ASSERT(m_numParametersForConstruct);
588     m_numCapturedVariables = m_codeBlockForConstruct->m_numCapturedVars;
589     m_symbolTable = m_codeBlockForConstruct->sharedSymbolTable();
590
591 #if ENABLE(JIT)
592     JSGlobalData* globalData = scopeChainNode->globalData;
593     if (globalData->canUseJIT()) {
594         bool dfgCompiled = false;
595         if (jitType == JITCode::DFGJIT)
596             dfgCompiled = DFG::tryCompileFunction(exec, m_codeBlockForConstruct.get(), m_jitCodeForConstruct, m_jitCodeForConstructWithArityCheck);
597         if (dfgCompiled) {
598             if (m_codeBlockForConstruct->alternative())
599                 m_codeBlockForConstruct->alternative()->unlinkIncomingCalls();
600         } else {
601             if (m_codeBlockForConstruct->alternative()) {
602                 m_codeBlockForConstruct = static_pointer_cast<FunctionCodeBlock>(m_codeBlockForConstruct->releaseAlternative());
603                 m_symbolTable = m_codeBlockForConstruct->sharedSymbolTable();
604                 return 0;
605             }
606             m_jitCodeForConstruct = JIT::compile(globalData, m_codeBlockForConstruct.get(), &m_jitCodeForConstructWithArityCheck);
607         }
608 #if !ENABLE(OPCODE_SAMPLING)
609         if (!BytecodeGenerator::dumpsGeneratedCode())
610             m_codeBlockForConstruct->handleBytecodeDiscardingOpportunity();
611 #endif
612         
613         m_codeBlockForConstruct->setJITCode(m_jitCodeForConstruct, m_jitCodeForConstructWithArityCheck);
614     }
615 #endif
616
617 #if ENABLE(JIT)
618 #if ENABLE(INTERPRETER)
619     if (!m_jitCodeForConstruct)
620         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
621     else
622 #endif
623     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct) + m_jitCodeForConstruct.size());
624 #else
625     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
626 #endif
627
628     return 0;
629 }
630
631 #if ENABLE(JIT)
632 void FunctionExecutable::jettisonOptimizedCodeForCall(JSGlobalData& globalData)
633 {
634     jettisonCodeBlock(globalData, m_codeBlockForCall);
635     m_jitCodeForCall = m_codeBlockForCall->getJITCode();
636     m_jitCodeForCallWithArityCheck = m_codeBlockForCall->getJITCodeWithArityCheck();
637 }
638
639 void FunctionExecutable::jettisonOptimizedCodeForConstruct(JSGlobalData& globalData)
640 {
641     jettisonCodeBlock(globalData, m_codeBlockForConstruct);
642     m_jitCodeForConstruct = m_codeBlockForConstruct->getJITCode();
643     m_jitCodeForConstructWithArityCheck = m_codeBlockForConstruct->getJITCodeWithArityCheck();
644 }
645 #endif
646
647 void FunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
648 {
649     FunctionExecutable* thisObject = jsCast<FunctionExecutable*>(cell);
650     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
651     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
652     ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
653     ScriptExecutable::visitChildren(thisObject, visitor);
654     if (thisObject->m_nameValue)
655         visitor.append(&thisObject->m_nameValue);
656     if (thisObject->m_codeBlockForCall)
657         thisObject->m_codeBlockForCall->visitAggregate(visitor);
658     if (thisObject->m_codeBlockForConstruct)
659         thisObject->m_codeBlockForConstruct->visitAggregate(visitor);
660 }
661
662 void FunctionExecutable::discardCode()
663 {
664 #if ENABLE(JIT)
665     // These first two checks are to handle the rare case where
666     // we are trying to evict code for a function during its
667     // codegen.
668     if (!m_jitCodeForCall && m_codeBlockForCall)
669         return;
670     if (!m_jitCodeForConstruct && m_codeBlockForConstruct)
671         return;
672 #endif
673     clearCode();
674 }
675
676 void FunctionExecutable::finalize(JSCell* cell)
677 {
678     jsCast<FunctionExecutable*>(cell)->clearCode();
679 }
680
681 inline void FunctionExecutable::clearCode()
682 {
683     if (m_codeBlockForCall) {
684         m_codeBlockForCall->clearEvalCache();
685         m_codeBlockForCall.clear();
686     }
687     if (m_codeBlockForConstruct) {
688         m_codeBlockForConstruct->clearEvalCache();
689         m_codeBlockForConstruct.clear();
690     }
691     Base::clearCode();
692 }
693
694 void FunctionExecutable::unlinkCalls()
695 {
696 #if ENABLE(JIT)
697     if (!!m_jitCodeForCall) {
698         ASSERT(m_codeBlockForCall);
699         m_codeBlockForCall->unlinkCalls();
700     }
701     if (!!m_jitCodeForConstruct) {
702         ASSERT(m_codeBlockForConstruct);
703         m_codeBlockForConstruct->unlinkCalls();
704     }
705 #endif
706 }
707
708 FunctionExecutable* FunctionExecutable::fromGlobalCode(const Identifier& functionName, ExecState* exec, Debugger* debugger, const SourceCode& source, JSObject** exception)
709 {
710     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
711     RefPtr<ProgramNode> program = parse<ProgramNode>(&exec->globalData(), lexicalGlobalObject, source, 0, JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, debugger, exec, exception);
712     if (!program) {
713         ASSERT(*exception);
714         return 0;
715     }
716
717     // Uses of this function that would not result in a single function expression are invalid.
718     StatementNode* exprStatement = program->singleStatement();
719     ASSERT(exprStatement);
720     ASSERT(exprStatement->isExprStatement());
721     ExpressionNode* funcExpr = static_cast<ExprStatementNode*>(exprStatement)->expr();
722     ASSERT(funcExpr);
723     ASSERT(funcExpr->isFuncExprNode());
724     FunctionBodyNode* body = static_cast<FuncExprNode*>(funcExpr)->body();
725     ASSERT(body);
726
727     return FunctionExecutable::create(exec->globalData(), functionName, body->source(), body->usesArguments(), body->parameters(), body->isStrictMode(), body->lineNo(), body->lastLine());
728 }
729
730 UString FunctionExecutable::paramString() const
731 {
732     FunctionParameters& parameters = *m_parameters;
733     UStringBuilder builder;
734     for (size_t pos = 0; pos < parameters.size(); ++pos) {
735         if (!builder.isEmpty())
736             builder.append(", ");
737         builder.append(parameters[pos].ustring());
738     }
739     return builder.toUString();
740 }
741
742 }