2 * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "Executable.h"
29 #include "BytecodeGenerator.h"
30 #include "CodeBlock.h"
31 #include "DFGDriver.h"
34 #include "UStringBuilder.h"
39 const ClassInfo ExecutableBase::s_info = { "Executable", 0, 0, 0, CREATE_METHOD_TABLE(ExecutableBase) };
41 inline void ExecutableBase::clearCode()
44 m_jitCodeForCall.clear();
45 m_jitCodeForConstruct.clear();
46 m_jitCodeForCallWithArityCheck = MacroAssemblerCodePtr();
47 m_jitCodeForConstructWithArityCheck = MacroAssemblerCodePtr();
49 m_numParametersForCall = NUM_PARAMETERS_NOT_COMPILED;
50 m_numParametersForConstruct = NUM_PARAMETERS_NOT_COMPILED;
54 DFG::Intrinsic ExecutableBase::intrinsic() const
56 if (const NativeExecutable* nativeExecutable = jsDynamicCast<const NativeExecutable*>(this))
57 return nativeExecutable->intrinsic();
58 return DFG::NoIntrinsic;
62 const ClassInfo NativeExecutable::s_info = { "NativeExecutable", &ExecutableBase::s_info, 0, 0, CREATE_METHOD_TABLE(NativeExecutable) };
64 NativeExecutable::~NativeExecutable()
69 DFG::Intrinsic NativeExecutable::intrinsic() const
76 // Utility method used for jettisoning code blocks.
78 static void jettisonCodeBlock(JSGlobalData& globalData, OwnPtr<T>& codeBlock)
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()));
89 void NativeExecutable::finalize(JSCell* cell)
91 jsCast<NativeExecutable*>(cell)->clearCode();
94 const ClassInfo ScriptExecutable::s_info = { "ScriptExecutable", &ExecutableBase::s_info, 0, 0, CREATE_METHOD_TABLE(ScriptExecutable) };
96 const ClassInfo EvalExecutable::s_info = { "EvalExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(EvalExecutable) };
98 EvalExecutable::EvalExecutable(ExecState* exec, const SourceCode& source, bool inStrictContext)
99 : ScriptExecutable(exec->globalData().evalExecutableStructure.get(), exec, source, inStrictContext)
103 EvalExecutable::~EvalExecutable()
107 const ClassInfo ProgramExecutable::s_info = { "ProgramExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(ProgramExecutable) };
109 ProgramExecutable::ProgramExecutable(ExecState* exec, const SourceCode& source)
110 : ScriptExecutable(exec->globalData().programExecutableStructure.get(), exec, source, false)
114 ProgramExecutable::~ProgramExecutable()
118 const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(FunctionExecutable) };
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)
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)
140 FunctionExecutable::~FunctionExecutable()
144 JSObject* EvalExecutable::compileOptimized(ExecState* exec, ScopeChainNode* scopeChainNode)
146 ASSERT(exec->globalData().dynamicGlobalObject);
147 ASSERT(!!m_evalCodeBlock);
149 if (m_evalCodeBlock->getJITType() != JITCode::topTierJIT())
150 error = compileInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_evalCodeBlock->getJITType()));
151 ASSERT(!!m_evalCodeBlock);
155 JSObject* EvalExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
157 SamplingRegion samplingRegion(jitType == JITCode::BaselineJIT ? "Baseline Compilation (TOTAL)" : "DFG Compilation (TOTAL)");
160 UNUSED_PARAM(jitType);
162 JSObject* exception = 0;
163 JSGlobalData* globalData = &exec->globalData();
164 JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
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();
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);
179 recordParse(evalNode->features(), evalNode->hasCapturedVariables(), evalNode->lineNo(), evalNode->lastLine());
181 JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
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();
193 evalNode->destroyData();
194 m_evalCodeBlock->copyPostParseDataFromAlternative();
198 if (exec->globalData().canUseJIT()) {
199 bool dfgCompiled = false;
200 if (jitType == JITCode::DFGJIT)
201 dfgCompiled = DFG::tryCompile(exec, m_evalCodeBlock.get(), m_jitCodeForCall);
203 ASSERT(!m_evalCodeBlock->alternative() || !m_evalCodeBlock->alternative()->hasIncomingCalls());
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());
211 m_jitCodeForCall = JIT::compile(scopeChainNode->globalData, m_evalCodeBlock.get());
213 #if !ENABLE(OPCODE_SAMPLING)
214 if (!BytecodeGenerator::dumpsGeneratedCode())
215 m_evalCodeBlock->handleBytecodeDiscardingOpportunity();
217 m_evalCodeBlock->setJITCode(m_jitCodeForCall, MacroAssemblerCodePtr());
222 #if ENABLE(INTERPRETER)
223 if (!m_jitCodeForCall)
224 Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
227 Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock) + m_jitCodeForCall.size());
229 Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
236 void EvalExecutable::jettisonOptimizedCode(JSGlobalData& globalData)
238 jettisonCodeBlock(globalData, m_evalCodeBlock);
239 m_jitCodeForCall = m_evalCodeBlock->getJITCode();
240 ASSERT(!m_jitCodeForCallWithArityCheck);
244 void EvalExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
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);
255 void EvalExecutable::unlinkCalls()
258 if (!m_jitCodeForCall)
260 ASSERT(m_evalCodeBlock);
261 m_evalCodeBlock->unlinkCalls();
265 void EvalExecutable::finalize(JSCell* cell)
267 jsCast<EvalExecutable*>(cell)->clearCode();
270 inline void EvalExecutable::clearCode()
272 if (m_evalCodeBlock) {
273 m_evalCodeBlock->clearEvalCache();
274 m_evalCodeBlock.clear();
279 JSObject* ProgramExecutable::checkSyntax(ExecState* exec)
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);
291 JSObject* ProgramExecutable::compileOptimized(ExecState* exec, ScopeChainNode* scopeChainNode)
293 ASSERT(exec->globalData().dynamicGlobalObject);
294 ASSERT(!!m_programCodeBlock);
296 if (m_programCodeBlock->getJITType() != JITCode::topTierJIT())
297 error = compileInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_programCodeBlock->getJITType()));
298 ASSERT(!!m_programCodeBlock);
302 JSObject* ProgramExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
304 SamplingRegion samplingRegion(jitType == JITCode::BaselineJIT ? "Baseline Compilation (TOTAL)" : "DFG Compilation (TOTAL)");
307 UNUSED_PARAM(jitType);
309 JSObject* exception = 0;
310 JSGlobalData* globalData = &exec->globalData();
311 JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
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();
319 RefPtr<ProgramNode> programNode = parse<ProgramNode>(globalData, lexicalGlobalObject, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, lexicalGlobalObject->debugger(), exec, &exception);
324 recordParse(programNode->features(), programNode->hasCapturedVariables(), programNode->lineNo(), programNode->lastLine());
326 JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
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();
338 programNode->destroyData();
339 m_programCodeBlock->copyPostParseDataFromAlternative();
343 if (exec->globalData().canUseJIT()) {
344 bool dfgCompiled = false;
345 if (jitType == JITCode::DFGJIT)
346 dfgCompiled = DFG::tryCompile(exec, m_programCodeBlock.get(), m_jitCodeForCall);
348 if (m_programCodeBlock->alternative())
349 m_programCodeBlock->alternative()->unlinkIncomingCalls();
351 if (m_programCodeBlock->alternative()) {
352 m_programCodeBlock = static_pointer_cast<ProgramCodeBlock>(m_programCodeBlock->releaseAlternative());
355 m_jitCodeForCall = JIT::compile(scopeChainNode->globalData, m_programCodeBlock.get());
357 #if !ENABLE(OPCODE_SAMPLING)
358 if (!BytecodeGenerator::dumpsGeneratedCode())
359 m_programCodeBlock->handleBytecodeDiscardingOpportunity();
361 m_programCodeBlock->setJITCode(m_jitCodeForCall, MacroAssemblerCodePtr());
366 #if ENABLE(INTERPRETER)
367 if (!m_jitCodeForCall)
368 Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
371 Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock) + m_jitCodeForCall.size());
373 Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
380 void ProgramExecutable::jettisonOptimizedCode(JSGlobalData& globalData)
382 jettisonCodeBlock(globalData, m_programCodeBlock);
383 m_jitCodeForCall = m_programCodeBlock->getJITCode();
384 ASSERT(!m_jitCodeForCallWithArityCheck);
388 void ProgramExecutable::unlinkCalls()
391 if (!m_jitCodeForCall)
393 ASSERT(m_programCodeBlock);
394 m_programCodeBlock->unlinkCalls();
398 void ProgramExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
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);
409 void ProgramExecutable::finalize(JSCell* cell)
411 jsCast<ProgramExecutable*>(cell)->clearCode();
414 inline void ProgramExecutable::clearCode()
416 if (m_programCodeBlock) {
417 m_programCodeBlock->clearEvalCache();
418 m_programCodeBlock.clear();
423 FunctionCodeBlock* FunctionExecutable::baselineCodeBlockFor(CodeSpecializationKind kind)
425 FunctionCodeBlock* result;
426 if (kind == CodeForCall)
427 result = m_codeBlockForCall.get();
429 ASSERT(kind == CodeForConstruct);
430 result = m_codeBlockForConstruct.get();
434 while (result->alternative())
435 result = static_cast<FunctionCodeBlock*>(result->alternative());
437 ASSERT(result->getJITType() == JITCode::BaselineJIT);
441 JSObject* FunctionExecutable::compileOptimizedForCall(ExecState* exec, ScopeChainNode* scopeChainNode)
443 ASSERT(exec->globalData().dynamicGlobalObject);
444 ASSERT(!!m_codeBlockForCall);
446 if (m_codeBlockForCall->getJITType() != JITCode::topTierJIT())
447 error = compileForCallInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_codeBlockForCall->getJITType()));
448 ASSERT(!!m_codeBlockForCall);
452 JSObject* FunctionExecutable::compileOptimizedForConstruct(ExecState* exec, ScopeChainNode* scopeChainNode)
454 ASSERT(exec->globalData().dynamicGlobalObject);
455 ASSERT(!!m_codeBlockForConstruct);
457 if (m_codeBlockForConstruct->getJITType() != JITCode::topTierJIT())
458 error = compileForConstructInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_codeBlockForConstruct->getJITType()));
459 ASSERT(!!m_codeBlockForConstruct);
463 FunctionCodeBlock* FunctionExecutable::codeBlockWithBytecodeFor(CodeSpecializationKind kind)
465 FunctionCodeBlock* codeBlock = baselineCodeBlockFor(kind);
466 if (codeBlock->canProduceCopyWithBytecode())
471 PassOwnPtr<FunctionCodeBlock> FunctionExecutable::produceCodeBlockFor(ScopeChainNode* scopeChainNode, CompilationKind compilationKind, CodeSpecializationKind specializationKind, JSObject*& exception)
473 if (!!codeBlockFor(specializationKind) && codeBlockFor(specializationKind)->canProduceCopyWithBytecode()) {
474 BytecodeDestructionBlocker blocker(codeBlockFor(specializationKind).get());
475 return adoptPtr(new FunctionCodeBlock(CodeBlock::CopyParsedBlock, *codeBlockFor(specializationKind)));
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);
487 if (m_forceUsesArguments)
488 body->setUsesArguments();
489 body->finishParsing(m_parameters, m_name);
490 recordParse(body->features(), body->hasCapturedVariables(), body->lineNo(), body->lastLine());
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();
501 result->copyPostParseDataFrom(codeBlockFor(specializationKind).get());
502 return result.release();
505 JSObject* FunctionExecutable::compileForCallInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
507 SamplingRegion samplingRegion(jitType == JITCode::BaselineJIT ? "Baseline Compilation (TOTAL)" : "DFG Compilation (TOTAL)");
511 UNUSED_PARAM(jitType);
514 ASSERT((jitType == JITCode::bottomTierJIT()) == !m_codeBlockForCall);
516 OwnPtr<FunctionCodeBlock> newCodeBlock = produceCodeBlockFor(scopeChainNode, !!m_codeBlockForCall ? OptimizingCompilation : FirstCompilation, CodeForCall, exception);
520 newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_codeBlockForCall.release()));
521 m_codeBlockForCall = newCodeBlock.release();
523 m_numParametersForCall = m_codeBlockForCall->m_numParameters;
524 ASSERT(m_numParametersForCall);
525 m_numCapturedVariables = m_codeBlockForCall->m_numCapturedVars;
526 m_symbolTable = m_codeBlockForCall->sharedSymbolTable();
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);
535 if (m_codeBlockForCall->alternative())
536 m_codeBlockForCall->alternative()->unlinkIncomingCalls();
538 if (m_codeBlockForCall->alternative()) {
539 m_codeBlockForCall = static_pointer_cast<FunctionCodeBlock>(m_codeBlockForCall->releaseAlternative());
540 m_symbolTable = m_codeBlockForCall->sharedSymbolTable();
543 m_jitCodeForCall = JIT::compile(globalData, m_codeBlockForCall.get(), &m_jitCodeForCallWithArityCheck);
545 #if !ENABLE(OPCODE_SAMPLING)
546 if (!BytecodeGenerator::dumpsGeneratedCode())
547 m_codeBlockForCall->handleBytecodeDiscardingOpportunity();
550 m_codeBlockForCall->setJITCode(m_jitCodeForCall, m_jitCodeForCallWithArityCheck);
555 #if ENABLE(INTERPRETER)
556 if (!m_jitCodeForCall)
557 Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
560 Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall) + m_jitCodeForCall.size());
562 Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
568 JSObject* FunctionExecutable::compileForConstructInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
570 SamplingRegion samplingRegion(jitType == JITCode::BaselineJIT ? "Baseline Compilation (TOTAL)" : "DFG Compilation (TOTAL)");
573 UNUSED_PARAM(jitType);
577 ASSERT((jitType == JITCode::bottomTierJIT()) == !m_codeBlockForConstruct);
579 OwnPtr<FunctionCodeBlock> newCodeBlock = produceCodeBlockFor(scopeChainNode, !!m_codeBlockForConstruct ? OptimizingCompilation : FirstCompilation, CodeForConstruct, exception);
583 newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_codeBlockForConstruct.release()));
584 m_codeBlockForConstruct = newCodeBlock.release();
586 m_numParametersForConstruct = m_codeBlockForConstruct->m_numParameters;
587 ASSERT(m_numParametersForConstruct);
588 m_numCapturedVariables = m_codeBlockForConstruct->m_numCapturedVars;
589 m_symbolTable = m_codeBlockForConstruct->sharedSymbolTable();
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);
598 if (m_codeBlockForConstruct->alternative())
599 m_codeBlockForConstruct->alternative()->unlinkIncomingCalls();
601 if (m_codeBlockForConstruct->alternative()) {
602 m_codeBlockForConstruct = static_pointer_cast<FunctionCodeBlock>(m_codeBlockForConstruct->releaseAlternative());
603 m_symbolTable = m_codeBlockForConstruct->sharedSymbolTable();
606 m_jitCodeForConstruct = JIT::compile(globalData, m_codeBlockForConstruct.get(), &m_jitCodeForConstructWithArityCheck);
608 #if !ENABLE(OPCODE_SAMPLING)
609 if (!BytecodeGenerator::dumpsGeneratedCode())
610 m_codeBlockForConstruct->handleBytecodeDiscardingOpportunity();
613 m_codeBlockForConstruct->setJITCode(m_jitCodeForConstruct, m_jitCodeForConstructWithArityCheck);
618 #if ENABLE(INTERPRETER)
619 if (!m_jitCodeForConstruct)
620 Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
623 Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct) + m_jitCodeForConstruct.size());
625 Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
632 void FunctionExecutable::jettisonOptimizedCodeForCall(JSGlobalData& globalData)
634 jettisonCodeBlock(globalData, m_codeBlockForCall);
635 m_jitCodeForCall = m_codeBlockForCall->getJITCode();
636 m_jitCodeForCallWithArityCheck = m_codeBlockForCall->getJITCodeWithArityCheck();
639 void FunctionExecutable::jettisonOptimizedCodeForConstruct(JSGlobalData& globalData)
641 jettisonCodeBlock(globalData, m_codeBlockForConstruct);
642 m_jitCodeForConstruct = m_codeBlockForConstruct->getJITCode();
643 m_jitCodeForConstructWithArityCheck = m_codeBlockForConstruct->getJITCodeWithArityCheck();
647 void FunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
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);
662 void FunctionExecutable::discardCode()
665 // These first two checks are to handle the rare case where
666 // we are trying to evict code for a function during its
668 if (!m_jitCodeForCall && m_codeBlockForCall)
670 if (!m_jitCodeForConstruct && m_codeBlockForConstruct)
676 void FunctionExecutable::finalize(JSCell* cell)
678 jsCast<FunctionExecutable*>(cell)->clearCode();
681 inline void FunctionExecutable::clearCode()
683 if (m_codeBlockForCall) {
684 m_codeBlockForCall->clearEvalCache();
685 m_codeBlockForCall.clear();
687 if (m_codeBlockForConstruct) {
688 m_codeBlockForConstruct->clearEvalCache();
689 m_codeBlockForConstruct.clear();
694 void FunctionExecutable::unlinkCalls()
697 if (!!m_jitCodeForCall) {
698 ASSERT(m_codeBlockForCall);
699 m_codeBlockForCall->unlinkCalls();
701 if (!!m_jitCodeForConstruct) {
702 ASSERT(m_codeBlockForConstruct);
703 m_codeBlockForConstruct->unlinkCalls();
708 FunctionExecutable* FunctionExecutable::fromGlobalCode(const Identifier& functionName, ExecState* exec, Debugger* debugger, const SourceCode& source, JSObject** exception)
710 JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
711 RefPtr<ProgramNode> program = parse<ProgramNode>(&exec->globalData(), lexicalGlobalObject, source, 0, JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, debugger, exec, exception);
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();
723 ASSERT(funcExpr->isFuncExprNode());
724 FunctionBodyNode* body = static_cast<FuncExprNode*>(funcExpr)->body();
727 return FunctionExecutable::create(exec->globalData(), functionName, body->source(), body->usesArguments(), body->parameters(), body->isStrictMode(), body->lineNo(), body->lastLine());
730 UString FunctionExecutable::paramString() const
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());
739 return builder.toUString();