2 * Copyright (C) 2009, 2010, 2013 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 "BatchedTransitionOptimizer.h"
30 #include "BytecodeGenerator.h"
31 #include "CodeBlock.h"
32 #include "DFGDriver.h"
34 #include "Operations.h"
36 #include <wtf/Vector.h>
37 #include <wtf/text/StringBuilder.h>
41 const ClassInfo ExecutableBase::s_info = { "Executable", 0, 0, 0, CREATE_METHOD_TABLE(ExecutableBase) };
44 void ExecutableBase::destroy(JSCell* cell)
46 static_cast<ExecutableBase*>(cell)->ExecutableBase::~ExecutableBase();
50 void ExecutableBase::clearCode()
53 m_jitCodeForCall.clear();
54 m_jitCodeForConstruct.clear();
55 m_jitCodeForCallWithArityCheck = MacroAssemblerCodePtr();
56 m_jitCodeForConstructWithArityCheck = MacroAssemblerCodePtr();
58 m_numParametersForCall = NUM_PARAMETERS_NOT_COMPILED;
59 m_numParametersForConstruct = NUM_PARAMETERS_NOT_COMPILED;
63 Intrinsic ExecutableBase::intrinsic() const
65 if (const NativeExecutable* nativeExecutable = jsDynamicCast<const NativeExecutable*>(this))
66 return nativeExecutable->intrinsic();
70 Intrinsic ExecutableBase::intrinsic() const
76 const ClassInfo NativeExecutable::s_info = { "NativeExecutable", &ExecutableBase::s_info, 0, 0, CREATE_METHOD_TABLE(NativeExecutable) };
79 void NativeExecutable::destroy(JSCell* cell)
81 static_cast<NativeExecutable*>(cell)->NativeExecutable::~NativeExecutable();
86 Intrinsic NativeExecutable::intrinsic() const
93 // Utility method used for jettisoning code blocks.
95 static void jettisonCodeBlock(VM& vm, RefPtr<T>& codeBlock)
97 ASSERT(JITCode::isOptimizingJIT(codeBlock->jitType()));
98 ASSERT(codeBlock->alternative());
99 RefPtr<T> codeBlockToJettison = codeBlock.release();
100 codeBlock = static_pointer_cast<T>(codeBlockToJettison->releaseAlternative());
101 codeBlockToJettison->unlinkIncomingCalls();
102 vm.heap.jettisonDFGCodeBlock(static_pointer_cast<CodeBlock>(codeBlockToJettison.release()));
106 const ClassInfo ScriptExecutable::s_info = { "ScriptExecutable", &ExecutableBase::s_info, 0, 0, CREATE_METHOD_TABLE(ScriptExecutable) };
109 void ScriptExecutable::destroy(JSCell* cell)
111 static_cast<ScriptExecutable*>(cell)->ScriptExecutable::~ScriptExecutable();
115 void ScriptExecutable::installCode(CodeBlock* genericCodeBlock)
117 RELEASE_ASSERT(genericCodeBlock->ownerExecutable() == this);
119 VM& vm = *genericCodeBlock->vm();
121 if (vm.m_perBytecodeProfiler)
122 vm.m_perBytecodeProfiler->ensureBytecodesFor(genericCodeBlock);
124 ASSERT(vm.heap.isDeferred());
126 if (JITCode::isJIT(genericCodeBlock->jitType())) {
127 vm.heap.reportExtraMemoryCost(
128 sizeof(CodeBlock) + genericCodeBlock->jitCode()->size());
130 vm.heap.reportExtraMemoryCost(sizeof(CodeBlock));
132 CodeSpecializationKind kind = genericCodeBlock->specializationKind();
134 RefPtr<CodeBlock> oldCodeBlock;
138 m_jitCodeForCall = genericCodeBlock->jitCode();
139 m_jitCodeForCallWithArityCheck = genericCodeBlock->jitCodeWithArityCheck();
140 m_numParametersForCall = genericCodeBlock->numParameters();
142 case CodeForConstruct:
143 m_jitCodeForConstruct = genericCodeBlock->jitCode();
144 m_jitCodeForConstructWithArityCheck = genericCodeBlock->jitCodeWithArityCheck();
145 m_numParametersForConstruct = genericCodeBlock->numParameters();
149 switch (genericCodeBlock->codeType()) {
151 ProgramExecutable* executable = jsCast<ProgramExecutable*>(this);
152 ProgramCodeBlock* codeBlock = static_cast<ProgramCodeBlock*>(genericCodeBlock);
154 ASSERT(!codeBlock->jitCodeWithArityCheck());
155 ASSERT(kind == CodeForCall);
157 oldCodeBlock = executable->m_programCodeBlock;
158 executable->m_programCodeBlock = codeBlock;
163 EvalExecutable* executable = jsCast<EvalExecutable*>(this);
164 EvalCodeBlock* codeBlock = static_cast<EvalCodeBlock*>(genericCodeBlock);
166 ASSERT(!codeBlock->jitCodeWithArityCheck());
167 ASSERT(kind == CodeForCall);
169 oldCodeBlock = executable->m_evalCodeBlock;
170 executable->m_evalCodeBlock = codeBlock;
175 FunctionExecutable* executable = jsCast<FunctionExecutable*>(this);
176 FunctionCodeBlock* codeBlock = static_cast<FunctionCodeBlock*>(genericCodeBlock);
180 oldCodeBlock = executable->m_codeBlockForCall;
181 executable->m_codeBlockForCall = codeBlock;
183 case CodeForConstruct:
184 oldCodeBlock = executable->m_codeBlockForConstruct;
185 executable->m_codeBlockForConstruct = codeBlock;
192 oldCodeBlock->unlinkIncomingCalls();
195 PassRefPtr<CodeBlock> ScriptExecutable::newCodeBlockFor(
196 CodeSpecializationKind kind, JSScope* scope, JSObject*& exception)
198 VM* vm = scope->vm();
200 ASSERT(vm->heap.isDeferred());
202 if (classInfo() == EvalExecutable::info()) {
203 EvalExecutable* executable = jsCast<EvalExecutable*>(this);
204 RELEASE_ASSERT(kind == CodeForCall);
205 RELEASE_ASSERT(!executable->m_evalCodeBlock);
206 return adoptRef(new EvalCodeBlock(
207 executable, executable->m_unlinkedEvalCodeBlock.get(), scope,
208 executable->source().provider()));
211 if (classInfo() == ProgramExecutable::info()) {
212 ProgramExecutable* executable = jsCast<ProgramExecutable*>(this);
213 RELEASE_ASSERT(kind == CodeForCall);
214 RELEASE_ASSERT(!executable->m_programCodeBlock);
215 return adoptRef(new ProgramCodeBlock(
216 executable, executable->m_unlinkedProgramCodeBlock.get(), scope,
217 executable->source().provider(), executable->source().startColumn()));
220 RELEASE_ASSERT(classInfo() == FunctionExecutable::info());
221 FunctionExecutable* executable = jsCast<FunctionExecutable*>(this);
222 RELEASE_ASSERT(!executable->codeBlockFor(kind));
223 JSGlobalObject* globalObject = scope->globalObject();
225 DebuggerMode debuggerMode = globalObject->hasDebugger() ? DebuggerOn : DebuggerOff;
226 ProfilerMode profilerMode = globalObject->hasProfiler() ? ProfilerOn : ProfilerOff;
227 UnlinkedFunctionCodeBlock* unlinkedCodeBlock =
228 executable->m_unlinkedExecutable->codeBlockFor(
229 *vm, executable->m_source, kind, debuggerMode, profilerMode, error);
230 if (!unlinkedCodeBlock) {
231 exception = vm->throwException(
232 globalObject->globalExec(),
233 error.toErrorObject(globalObject, executable->m_source));
237 SourceProvider* provider = executable->source().provider();
238 unsigned sourceOffset = executable->source().startOffset();
239 unsigned startColumn = executable->source().startColumn();
241 return adoptRef(new FunctionCodeBlock(
242 executable, unlinkedCodeBlock, scope, provider, sourceOffset, startColumn));
245 PassRefPtr<CodeBlock> ScriptExecutable::newReplacementCodeBlockFor(
246 CodeSpecializationKind kind)
248 if (classInfo() == EvalExecutable::info()) {
249 RELEASE_ASSERT(kind == CodeForCall);
250 EvalExecutable* executable = jsCast<EvalExecutable*>(this);
251 RefPtr<EvalCodeBlock> result = adoptRef(new EvalCodeBlock(
252 CodeBlock::CopyParsedBlock, *executable->m_evalCodeBlock));
253 result->setAlternative(executable->m_evalCodeBlock);
257 if (classInfo() == ProgramExecutable::info()) {
258 RELEASE_ASSERT(kind == CodeForCall);
259 ProgramExecutable* executable = jsCast<ProgramExecutable*>(this);
260 RefPtr<ProgramCodeBlock> result = adoptRef(new ProgramCodeBlock(
261 CodeBlock::CopyParsedBlock, *executable->m_programCodeBlock));
262 result->setAlternative(executable->m_programCodeBlock);
266 RELEASE_ASSERT(classInfo() == FunctionExecutable::info());
267 FunctionExecutable* executable = jsCast<FunctionExecutable*>(this);
268 RefPtr<FunctionCodeBlock> result = adoptRef(new FunctionCodeBlock(
269 CodeBlock::CopyParsedBlock, *executable->codeBlockFor(kind)));
270 result->setAlternative(executable->codeBlockFor(kind));
274 JSObject* ScriptExecutable::prepareForExecutionImpl(
275 ExecState* exec, JSScope* scope, CodeSpecializationKind kind)
278 DeferGC deferGC(vm.heap);
280 JSObject* exception = 0;
281 RefPtr<CodeBlock> codeBlock = newCodeBlockFor(kind, scope, exception);
283 RELEASE_ASSERT(exception);
287 JITCode::JITType jitType;
289 jitType = JITCode::InterpreterThunk;
291 jitType = JITCode::BaselineJIT;
293 codeBlock->prepareForExecution(exec, jitType);
295 installCode(codeBlock.get());
299 const ClassInfo EvalExecutable::s_info = { "EvalExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(EvalExecutable) };
301 EvalExecutable* EvalExecutable::create(ExecState* exec, const SourceCode& source, bool isInStrictContext)
303 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
304 if (!globalObject->evalEnabled()) {
305 exec->vm().throwException(exec, createEvalError(exec, globalObject->evalDisabledErrorMessage()));
309 EvalExecutable* executable = new (NotNull, allocateCell<EvalExecutable>(*exec->heap())) EvalExecutable(exec, source, isInStrictContext);
310 executable->finishCreation(exec->vm());
312 UnlinkedEvalCodeBlock* unlinkedEvalCode = globalObject->createEvalCodeBlock(exec, executable);
313 if (!unlinkedEvalCode)
316 executable->m_unlinkedEvalCodeBlock.set(exec->vm(), executable, unlinkedEvalCode);
321 EvalExecutable::EvalExecutable(ExecState* exec, const SourceCode& source, bool inStrictContext)
322 : ScriptExecutable(exec->vm().evalExecutableStructure.get(), exec, source, inStrictContext)
326 void EvalExecutable::destroy(JSCell* cell)
328 static_cast<EvalExecutable*>(cell)->EvalExecutable::~EvalExecutable();
331 const ClassInfo ProgramExecutable::s_info = { "ProgramExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(ProgramExecutable) };
333 ProgramExecutable::ProgramExecutable(ExecState* exec, const SourceCode& source)
334 : ScriptExecutable(exec->vm().programExecutableStructure.get(), exec, source, false)
338 void ProgramExecutable::destroy(JSCell* cell)
340 static_cast<ProgramExecutable*>(cell)->ProgramExecutable::~ProgramExecutable();
343 const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(FunctionExecutable) };
345 FunctionExecutable::FunctionExecutable(VM& vm, const SourceCode& source, UnlinkedFunctionExecutable* unlinkedExecutable, unsigned firstLine, unsigned lastLine, unsigned startColumn)
346 : ScriptExecutable(vm.functionExecutableStructure.get(), vm, source, unlinkedExecutable->isInStrictContext())
347 , m_unlinkedExecutable(vm, this, unlinkedExecutable)
349 RELEASE_ASSERT(!source.isNull());
350 ASSERT(source.length());
351 m_firstLine = firstLine;
352 m_lastLine = lastLine;
353 m_startColumn = startColumn;
356 void FunctionExecutable::destroy(JSCell* cell)
358 static_cast<FunctionExecutable*>(cell)->FunctionExecutable::~FunctionExecutable();
361 inline const char* samplingDescription(JITCode::JITType jitType)
364 case JITCode::InterpreterThunk:
365 return "Interpreter Compilation (TOTAL)";
366 case JITCode::BaselineJIT:
367 return "Baseline Compilation (TOTAL)";
368 case JITCode::DFGJIT:
369 return "DFG Compilation (TOTAL)";
370 case JITCode::FTLJIT:
371 return "FTL Compilation (TOTAL)";
373 RELEASE_ASSERT_NOT_REACHED();
379 void EvalExecutable::jettisonOptimizedCode(VM& vm)
381 jettisonCodeBlock(vm, m_evalCodeBlock);
382 m_jitCodeForCall = m_evalCodeBlock->jitCode();
383 ASSERT(!m_jitCodeForCallWithArityCheck);
385 #endif // ENABLE(JIT)
387 void EvalExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
389 EvalExecutable* thisObject = jsCast<EvalExecutable*>(cell);
390 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
391 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
392 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
393 ScriptExecutable::visitChildren(thisObject, visitor);
394 if (thisObject->m_evalCodeBlock)
395 thisObject->m_evalCodeBlock->visitAggregate(visitor);
396 visitor.append(&thisObject->m_unlinkedEvalCodeBlock);
399 void EvalExecutable::unlinkCalls()
402 if (!m_jitCodeForCall)
404 RELEASE_ASSERT(m_evalCodeBlock);
405 m_evalCodeBlock->unlinkCalls();
409 void EvalExecutable::clearCode()
411 m_evalCodeBlock.clear();
412 m_unlinkedEvalCodeBlock.clear();
416 JSObject* ProgramExecutable::checkSyntax(ExecState* exec)
419 VM* vm = &exec->vm();
420 JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
421 RefPtr<ProgramNode> programNode = parse<ProgramNode>(vm, m_source, 0, Identifier(), JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, error);
424 ASSERT(error.m_type != ParserError::ErrorNone);
425 return error.toErrorObject(lexicalGlobalObject, m_source);
429 void ProgramExecutable::jettisonOptimizedCode(VM& vm)
431 jettisonCodeBlock(vm, m_programCodeBlock);
432 m_jitCodeForCall = m_programCodeBlock->jitCode();
433 ASSERT(!m_jitCodeForCallWithArityCheck);
437 void ProgramExecutable::unlinkCalls()
440 if (!m_jitCodeForCall)
442 RELEASE_ASSERT(m_programCodeBlock);
443 m_programCodeBlock->unlinkCalls();
447 JSObject* ProgramExecutable::initializeGlobalProperties(VM& vm, CallFrame* callFrame, JSScope* scope)
449 RELEASE_ASSERT(scope);
450 JSGlobalObject* globalObject = scope->globalObject();
451 RELEASE_ASSERT(globalObject);
452 ASSERT(&globalObject->vm() == &vm);
454 JSObject* exception = 0;
455 UnlinkedProgramCodeBlock* unlinkedCode = globalObject->createProgramCodeBlock(callFrame, this, &exception);
459 m_unlinkedProgramCodeBlock.set(vm, this, unlinkedCode);
461 BatchedTransitionOptimizer optimizer(vm, globalObject);
463 const UnlinkedProgramCodeBlock::VariableDeclations& variableDeclarations = unlinkedCode->variableDeclarations();
464 const UnlinkedProgramCodeBlock::FunctionDeclations& functionDeclarations = unlinkedCode->functionDeclarations();
466 CallFrame* globalExec = globalObject->globalExec();
468 for (size_t i = 0; i < functionDeclarations.size(); ++i) {
469 UnlinkedFunctionExecutable* unlinkedFunctionExecutable = functionDeclarations[i].second.get();
470 JSValue value = JSFunction::create(globalExec, unlinkedFunctionExecutable->link(vm, m_source, lineNo(), 0), scope);
471 globalObject->addFunction(callFrame, functionDeclarations[i].first, value);
474 for (size_t i = 0; i < variableDeclarations.size(); ++i) {
475 if (variableDeclarations[i].second & DeclarationStacks::IsConstant)
476 globalObject->addConst(callFrame, variableDeclarations[i].first);
478 globalObject->addVar(callFrame, variableDeclarations[i].first);
483 void ProgramExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
485 ProgramExecutable* thisObject = jsCast<ProgramExecutable*>(cell);
486 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
487 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
488 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
489 ScriptExecutable::visitChildren(thisObject, visitor);
490 visitor.append(&thisObject->m_unlinkedProgramCodeBlock);
491 if (thisObject->m_programCodeBlock)
492 thisObject->m_programCodeBlock->visitAggregate(visitor);
495 void ProgramExecutable::clearCode()
497 m_programCodeBlock.clear();
498 m_unlinkedProgramCodeBlock.clear();
502 FunctionCodeBlock* FunctionExecutable::baselineCodeBlockFor(CodeSpecializationKind kind)
504 FunctionCodeBlock* result;
505 if (kind == CodeForCall)
506 result = m_codeBlockForCall.get();
508 RELEASE_ASSERT(kind == CodeForConstruct);
509 result = m_codeBlockForConstruct.get();
513 while (result->alternative())
514 result = static_cast<FunctionCodeBlock*>(result->alternative());
515 RELEASE_ASSERT(result);
516 ASSERT(JITCode::isBaselineCode(result->jitType()));
521 void FunctionExecutable::jettisonOptimizedCodeForCall(VM& vm)
523 jettisonCodeBlock(vm, m_codeBlockForCall);
524 m_jitCodeForCall = m_codeBlockForCall->jitCode();
525 m_jitCodeForCallWithArityCheck = m_codeBlockForCall->jitCodeWithArityCheck();
528 void FunctionExecutable::jettisonOptimizedCodeForConstruct(VM& vm)
530 jettisonCodeBlock(vm, m_codeBlockForConstruct);
531 m_jitCodeForConstruct = m_codeBlockForConstruct->jitCode();
532 m_jitCodeForConstructWithArityCheck = m_codeBlockForConstruct->jitCodeWithArityCheck();
536 void FunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
538 FunctionExecutable* thisObject = jsCast<FunctionExecutable*>(cell);
539 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
540 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
541 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
542 ScriptExecutable::visitChildren(thisObject, visitor);
543 if (thisObject->m_codeBlockForCall)
544 thisObject->m_codeBlockForCall->visitAggregate(visitor);
545 if (thisObject->m_codeBlockForConstruct)
546 thisObject->m_codeBlockForConstruct->visitAggregate(visitor);
547 visitor.append(&thisObject->m_unlinkedExecutable);
550 void FunctionExecutable::clearCodeIfNotCompiling()
557 void FunctionExecutable::clearUnlinkedCodeForRecompilationIfNotCompiling()
561 m_unlinkedExecutable->clearCodeForRecompilation();
564 void FunctionExecutable::clearCode()
566 m_codeBlockForCall.clear();
567 m_codeBlockForConstruct.clear();
571 void FunctionExecutable::unlinkCalls()
574 if (!!m_jitCodeForCall) {
575 RELEASE_ASSERT(m_codeBlockForCall);
576 m_codeBlockForCall->unlinkCalls();
578 if (!!m_jitCodeForConstruct) {
579 RELEASE_ASSERT(m_codeBlockForConstruct);
580 m_codeBlockForConstruct->unlinkCalls();
585 FunctionExecutable* FunctionExecutable::fromGlobalCode(const Identifier& name, ExecState* exec, Debugger* debugger, const SourceCode& source, JSObject** exception)
587 UnlinkedFunctionExecutable* unlinkedFunction = UnlinkedFunctionExecutable::fromGlobalCode(name, exec, debugger, source, exception);
588 if (!unlinkedFunction)
590 unsigned firstLine = source.firstLine() + unlinkedFunction->firstLineOffset();
591 unsigned startOffset = source.startOffset() + unlinkedFunction->startOffset();
592 unsigned startColumn = source.startColumn();
593 unsigned sourceLength = unlinkedFunction->sourceLength();
594 SourceCode functionSource(source.provider(), startOffset, startOffset + sourceLength, firstLine, startColumn);
595 return FunctionExecutable::create(exec->vm(), functionSource, unlinkedFunction, firstLine, unlinkedFunction->lineCount(), startColumn);
598 String FunctionExecutable::paramString() const
600 return m_unlinkedExecutable->paramString();
603 CodeBlockHash ExecutableBase::hashFor(CodeSpecializationKind kind) const
605 if (this->classInfo() == NativeExecutable::info())
606 return jsCast<const NativeExecutable*>(this)->hashFor(kind);
608 return jsCast<const ScriptExecutable*>(this)->hashFor(kind);
611 CodeBlockHash NativeExecutable::hashFor(CodeSpecializationKind kind) const
613 if (kind == CodeForCall)
614 return CodeBlockHash(static_cast<unsigned>(bitwise_cast<size_t>(m_function)));
616 RELEASE_ASSERT(kind == CodeForConstruct);
617 return CodeBlockHash(static_cast<unsigned>(bitwise_cast<size_t>(m_constructor)));
620 CodeBlockHash ScriptExecutable::hashFor(CodeSpecializationKind kind) const
622 return CodeBlockHash(source(), kind);