/*
- * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
+ * Copyright (C) 2009, 2010, 2013, 2015 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
#include "config.h"
#include "Executable.h"
-#include "BytecodeGenerator.h"
+#include "BatchedTransitionOptimizer.h"
#include "CodeBlock.h"
+#include "DFGDriver.h"
#include "JIT.h"
+#include "JSCInlines.h"
+#include "JSFunctionNameScope.h"
+#include "LLIntEntrypoint.h"
#include "Parser.h"
-#include "UStringBuilder.h"
-#include "Vector.h"
-
-#if ENABLE(DFG_JIT)
-#include "DFGByteCodeParser.h"
-#include "DFGJITCompiler.h"
-#endif
+#include "ProfilerDatabase.h"
+#include "TypeProfiler.h"
+#include <wtf/CommaPrinter.h>
+#include <wtf/Vector.h>
+#include <wtf/text/StringBuilder.h>
namespace JSC {
-const ClassInfo ExecutableBase::s_info = { "Executable", 0, 0, 0 };
-
-#if ENABLE(JIT)
-class ExecutableFinalizer : public WeakHandleOwner {
- virtual void finalize(Handle<Unknown> handle, void*)
- {
- Weak<ExecutableBase> executable(Weak<ExecutableBase>::Adopt, handle);
- executable->clearExecutableCode();
- }
-};
+const ClassInfo ExecutableBase::s_info = { "Executable", 0, 0, CREATE_METHOD_TABLE(ExecutableBase) };
-WeakHandleOwner* ExecutableBase::executableFinalizer()
+void ExecutableBase::destroy(JSCell* cell)
{
- DEFINE_STATIC_LOCAL(ExecutableFinalizer, finalizer, ());
- return &finalizer;
+ static_cast<ExecutableBase*>(cell)->ExecutableBase::~ExecutableBase();
}
-#endif
-
-const ClassInfo NativeExecutable::s_info = { "NativeExecutable", &ExecutableBase::s_info, 0, 0 };
-NativeExecutable::~NativeExecutable()
+void ExecutableBase::clearCode()
{
+#if ENABLE(JIT)
+ m_jitCodeForCall = nullptr;
+ m_jitCodeForConstruct = nullptr;
+ m_jitCodeForCallWithArityCheck = MacroAssemblerCodePtr();
+ m_jitCodeForConstructWithArityCheck = MacroAssemblerCodePtr();
+ m_jitCodeForCallWithArityCheckAndPreserveRegs = MacroAssemblerCodePtr();
+ m_jitCodeForConstructWithArityCheckAndPreserveRegs = MacroAssemblerCodePtr();
+#endif
+ m_numParametersForCall = NUM_PARAMETERS_NOT_COMPILED;
+ m_numParametersForConstruct = NUM_PARAMETERS_NOT_COMPILED;
}
-const ClassInfo ScriptExecutable::s_info = { "ScriptExecutable", &ExecutableBase::s_info, 0, 0 };
+#if ENABLE(DFG_JIT)
+Intrinsic ExecutableBase::intrinsic() const
+{
+ if (const NativeExecutable* nativeExecutable = jsDynamicCast<const NativeExecutable*>(this))
+ return nativeExecutable->intrinsic();
+ return NoIntrinsic;
+}
+#else
+Intrinsic ExecutableBase::intrinsic() const
+{
+ return NoIntrinsic;
+}
+#endif
-const ClassInfo EvalExecutable::s_info = { "EvalExecutable", &ScriptExecutable::s_info, 0, 0 };
+const ClassInfo NativeExecutable::s_info = { "NativeExecutable", &ExecutableBase::s_info, 0, CREATE_METHOD_TABLE(NativeExecutable) };
-EvalExecutable::EvalExecutable(ExecState* exec, const SourceCode& source, bool inStrictContext)
- : ScriptExecutable(exec->globalData().evalExecutableStructure.get(), exec, source, inStrictContext)
+void NativeExecutable::destroy(JSCell* cell)
{
+ static_cast<NativeExecutable*>(cell)->NativeExecutable::~NativeExecutable();
}
-EvalExecutable::~EvalExecutable()
+#if ENABLE(DFG_JIT)
+Intrinsic NativeExecutable::intrinsic() const
{
+ return m_intrinsic;
}
+#endif
-const ClassInfo ProgramExecutable::s_info = { "ProgramExecutable", &ScriptExecutable::s_info, 0, 0 };
+const ClassInfo ScriptExecutable::s_info = { "ScriptExecutable", &ExecutableBase::s_info, 0, CREATE_METHOD_TABLE(ScriptExecutable) };
+
+ScriptExecutable::ScriptExecutable(Structure* structure, VM& vm, const SourceCode& source, bool isInStrictContext)
+ : ExecutableBase(vm, structure, NUM_PARAMETERS_NOT_COMPILED)
+ , m_source(source)
+ , m_features(isInStrictContext ? StrictModeFeature : 0)
+ , m_hasCapturedVariables(false)
+ , m_neverInline(false)
+ , m_didTryToEnterInLoop(false)
+ , m_overrideLineNumber(-1)
+ , m_firstLine(-1)
+ , m_lastLine(-1)
+ , m_startColumn(UINT_MAX)
+ , m_endColumn(UINT_MAX)
+ , m_typeProfilingStartOffset(UINT_MAX)
+ , m_typeProfilingEndOffset(UINT_MAX)
+{
+}
-ProgramExecutable::ProgramExecutable(ExecState* exec, const SourceCode& source)
- : ScriptExecutable(exec->globalData().programExecutableStructure.get(), exec, source, false)
+void ScriptExecutable::destroy(JSCell* cell)
{
+ static_cast<ScriptExecutable*>(cell)->ScriptExecutable::~ScriptExecutable();
}
-ProgramExecutable::~ProgramExecutable()
+void ScriptExecutable::installCode(CodeBlock* genericCodeBlock)
{
+ RELEASE_ASSERT(genericCodeBlock->ownerExecutable() == this);
+ RELEASE_ASSERT(JITCode::isExecutableScript(genericCodeBlock->jitType()));
+
+ if (Options::verboseOSR())
+ dataLog("Installing ", *genericCodeBlock, "\n");
+
+ VM& vm = *genericCodeBlock->vm();
+
+ if (vm.m_perBytecodeProfiler)
+ vm.m_perBytecodeProfiler->ensureBytecodesFor(genericCodeBlock);
+
+ ASSERT(vm.heap.isDeferred());
+
+ CodeSpecializationKind kind = genericCodeBlock->specializationKind();
+
+ RefPtr<CodeBlock> oldCodeBlock;
+
+ switch (kind) {
+ case CodeForCall:
+ m_jitCodeForCall = genericCodeBlock->jitCode();
+ m_jitCodeForCallWithArityCheck = MacroAssemblerCodePtr();
+ m_jitCodeForCallWithArityCheckAndPreserveRegs = MacroAssemblerCodePtr();
+ m_numParametersForCall = genericCodeBlock->numParameters();
+ break;
+ case CodeForConstruct:
+ m_jitCodeForConstruct = genericCodeBlock->jitCode();
+ m_jitCodeForConstructWithArityCheck = MacroAssemblerCodePtr();
+ m_jitCodeForConstructWithArityCheckAndPreserveRegs = MacroAssemblerCodePtr();
+ m_numParametersForConstruct = genericCodeBlock->numParameters();
+ break;
+ }
+
+ switch (genericCodeBlock->codeType()) {
+ case GlobalCode: {
+ ProgramExecutable* executable = jsCast<ProgramExecutable*>(this);
+ ProgramCodeBlock* codeBlock = static_cast<ProgramCodeBlock*>(genericCodeBlock);
+
+ ASSERT(kind == CodeForCall);
+
+ oldCodeBlock = executable->m_programCodeBlock;
+ executable->m_programCodeBlock = codeBlock;
+ break;
+ }
+
+ case EvalCode: {
+ EvalExecutable* executable = jsCast<EvalExecutable*>(this);
+ EvalCodeBlock* codeBlock = static_cast<EvalCodeBlock*>(genericCodeBlock);
+
+ ASSERT(kind == CodeForCall);
+
+ oldCodeBlock = executable->m_evalCodeBlock;
+ executable->m_evalCodeBlock = codeBlock;
+ break;
+ }
+
+ case FunctionCode: {
+ FunctionExecutable* executable = jsCast<FunctionExecutable*>(this);
+ FunctionCodeBlock* codeBlock = static_cast<FunctionCodeBlock*>(genericCodeBlock);
+
+ switch (kind) {
+ case CodeForCall:
+ oldCodeBlock = executable->m_codeBlockForCall;
+ executable->m_codeBlockForCall = codeBlock;
+ break;
+ case CodeForConstruct:
+ oldCodeBlock = executable->m_codeBlockForConstruct;
+ executable->m_codeBlockForConstruct = codeBlock;
+ break;
+ }
+ break;
+ } }
+
+ if (oldCodeBlock)
+ oldCodeBlock->unlinkIncomingCalls();
+
+ Debugger* debugger = genericCodeBlock->globalObject()->debugger();
+ if (debugger)
+ debugger->registerCodeBlock(genericCodeBlock);
+
+ Heap::heap(this)->writeBarrier(this);
}
-const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, 0, 0 };
+RefPtr<CodeBlock> ScriptExecutable::newCodeBlockFor(
+ CodeSpecializationKind kind, JSFunction* function, JSScope* scope, JSObject*& exception)
+{
+ VM* vm = scope->vm();
+
+ ASSERT(vm->heap.isDeferred());
+ ASSERT(startColumn() != UINT_MAX);
+ ASSERT(endColumn() != UINT_MAX);
+
+ if (classInfo() == EvalExecutable::info()) {
+ EvalExecutable* executable = jsCast<EvalExecutable*>(this);
+ RELEASE_ASSERT(kind == CodeForCall);
+ RELEASE_ASSERT(!executable->m_evalCodeBlock);
+ RELEASE_ASSERT(!function);
+ return adoptRef(new EvalCodeBlock(
+ executable, executable->m_unlinkedEvalCodeBlock.get(), scope,
+ executable->source().provider()));
+ }
+
+ if (classInfo() == ProgramExecutable::info()) {
+ ProgramExecutable* executable = jsCast<ProgramExecutable*>(this);
+ RELEASE_ASSERT(kind == CodeForCall);
+ RELEASE_ASSERT(!executable->m_programCodeBlock);
+ RELEASE_ASSERT(!function);
+ return adoptRef(new ProgramCodeBlock(
+ executable, executable->m_unlinkedProgramCodeBlock.get(), scope,
+ executable->source().provider(), executable->source().startColumn()));
+ }
+
+ RELEASE_ASSERT(classInfo() == FunctionExecutable::info());
+ RELEASE_ASSERT(function);
+ FunctionExecutable* executable = jsCast<FunctionExecutable*>(this);
+ RELEASE_ASSERT(!executable->codeBlockFor(kind));
+ JSGlobalObject* globalObject = scope->globalObject();
+ ParserError error;
+ DebuggerMode debuggerMode = globalObject->hasDebugger() ? DebuggerOn : DebuggerOff;
+ ProfilerMode profilerMode = globalObject->hasProfiler() ? ProfilerOn : ProfilerOff;
+ UnlinkedFunctionCodeBlock* unlinkedCodeBlock =
+ executable->m_unlinkedExecutable->codeBlockFor(
+ *vm, executable->m_source, kind, debuggerMode, profilerMode, error);
+ recordParse(executable->m_unlinkedExecutable->features(), executable->m_unlinkedExecutable->hasCapturedVariables(), firstLine(), lastLine(), startColumn(), endColumn());
+ if (!unlinkedCodeBlock) {
+ exception = vm->throwException(
+ globalObject->globalExec(),
+ error.toErrorObject(globalObject, executable->m_source));
+ return nullptr;
+ }
+
+ // Parsing reveals whether our function uses features that require a separate function name object in the scope chain.
+ // Be sure to add this scope before linking the bytecode because this scope will change the resolution depth of non-local variables.
+ if (functionNameIsInScope(executable->name(), executable->functionMode())
+ && functionNameScopeIsDynamic(executable->usesEval(), executable->isStrictMode())) {
+ // We shouldn't have to do this. But we do, because bytecode linking requires a real scope
+ // chain.
+ // FIXME: https://bugs.webkit.org/show_bug.cgi?id=141885
+ SymbolTable* symbolTable =
+ SymbolTable::createNameScopeTable(*vm, executable->name(), ReadOnly | DontDelete);
+ scope = JSFunctionNameScope::create(
+ *vm, scope->globalObject(), scope, symbolTable, function);
+ }
+
+ SourceProvider* provider = executable->source().provider();
+ unsigned sourceOffset = executable->source().startOffset();
+ unsigned startColumn = executable->source().startColumn();
-FunctionExecutable::FunctionExecutable(JSGlobalData* globalData, const Identifier& name, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool inStrictContext, int firstLine, int lastLine)
- : ScriptExecutable(globalData->functionExecutableStructure.get(), globalData, source, inStrictContext)
- , m_numCapturedVariables(0)
- , m_forceUsesArguments(forceUsesArguments)
- , m_parameters(parameters)
- , m_name(name)
- , m_symbolTable(0)
+ return adoptRef(new FunctionCodeBlock(
+ executable, unlinkedCodeBlock, scope, provider, sourceOffset, startColumn));
+}
+
+PassRefPtr<CodeBlock> ScriptExecutable::newReplacementCodeBlockFor(
+ CodeSpecializationKind kind)
{
- m_firstLine = firstLine;
- m_lastLine = lastLine;
+ if (classInfo() == EvalExecutable::info()) {
+ RELEASE_ASSERT(kind == CodeForCall);
+ EvalExecutable* executable = jsCast<EvalExecutable*>(this);
+ EvalCodeBlock* baseline = static_cast<EvalCodeBlock*>(
+ executable->m_evalCodeBlock->baselineVersion());
+ RefPtr<EvalCodeBlock> result = adoptRef(new EvalCodeBlock(
+ CodeBlock::CopyParsedBlock, *baseline));
+ result->setAlternative(baseline);
+ return result;
+ }
+
+ if (classInfo() == ProgramExecutable::info()) {
+ RELEASE_ASSERT(kind == CodeForCall);
+ ProgramExecutable* executable = jsCast<ProgramExecutable*>(this);
+ ProgramCodeBlock* baseline = static_cast<ProgramCodeBlock*>(
+ executable->m_programCodeBlock->baselineVersion());
+ RefPtr<ProgramCodeBlock> result = adoptRef(new ProgramCodeBlock(
+ CodeBlock::CopyParsedBlock, *baseline));
+ result->setAlternative(baseline);
+ return result;
+ }
+
+ RELEASE_ASSERT(classInfo() == FunctionExecutable::info());
+ FunctionExecutable* executable = jsCast<FunctionExecutable*>(this);
+ FunctionCodeBlock* baseline = static_cast<FunctionCodeBlock*>(
+ executable->codeBlockFor(kind)->baselineVersion());
+ RefPtr<FunctionCodeBlock> result = adoptRef(new FunctionCodeBlock(
+ CodeBlock::CopyParsedBlock, *baseline));
+ result->setAlternative(baseline);
+ return result;
}
-FunctionExecutable::FunctionExecutable(ExecState* exec, const Identifier& name, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool inStrictContext, int firstLine, int lastLine)
- : ScriptExecutable(exec->globalData().functionExecutableStructure.get(), exec, source, inStrictContext)
- , m_numCapturedVariables(0)
- , m_forceUsesArguments(forceUsesArguments)
- , m_parameters(parameters)
- , m_name(name)
- , m_symbolTable(0)
+static void setupLLInt(VM& vm, CodeBlock* codeBlock)
{
- m_firstLine = firstLine;
- m_lastLine = lastLine;
+ LLInt::setEntrypoint(vm, codeBlock);
}
+static void setupJIT(VM& vm, CodeBlock* codeBlock)
+{
+#if ENABLE(JIT)
+ CompilationResult result = JIT::compile(&vm, codeBlock, JITCompilationMustSucceed);
+ RELEASE_ASSERT(result == CompilationSuccessful);
+#else
+ UNUSED_PARAM(vm);
+ UNUSED_PARAM(codeBlock);
+ UNREACHABLE_FOR_PLATFORM();
+#endif
+}
-JSObject* EvalExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode)
+JSObject* ScriptExecutable::prepareForExecutionImpl(
+ ExecState* exec, JSFunction* function, JSScope* scope, CodeSpecializationKind kind)
{
+ VM& vm = exec->vm();
+ DeferGC deferGC(vm.heap);
+
JSObject* exception = 0;
- JSGlobalData* globalData = &exec->globalData();
- JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
- if (!lexicalGlobalObject->isEvalEnabled())
- return throwError(exec, createEvalError(exec, "Eval is disabled"));
- RefPtr<EvalNode> evalNode = globalData->parser->parse<EvalNode>(lexicalGlobalObject, lexicalGlobalObject->debugger(), exec, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, &exception);
- if (!evalNode) {
- ASSERT(exception);
+ RefPtr<CodeBlock> codeBlock = newCodeBlockFor(kind, function, scope, exception);
+ if (!codeBlock) {
+ RELEASE_ASSERT(exception);
return exception;
}
- recordParse(evalNode->features(), evalNode->hasCapturedVariables(), evalNode->lineNo(), evalNode->lastLine());
+
+ if (Options::validateBytecode())
+ codeBlock->validate();
+
+ if (Options::useLLInt())
+ setupLLInt(vm, codeBlock.get());
+ else
+ setupJIT(vm, codeBlock.get());
+
+ installCode(codeBlock.get());
+ return 0;
+}
- JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
+const ClassInfo EvalExecutable::s_info = { "EvalExecutable", &ScriptExecutable::s_info, 0, CREATE_METHOD_TABLE(EvalExecutable) };
- ASSERT(!m_evalCodeBlock);
- m_evalCodeBlock = adoptPtr(new EvalCodeBlock(this, globalObject, source().provider(), scopeChainNode->localDepth()));
- OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(evalNode.get(), scopeChainNode, m_evalCodeBlock->symbolTable(), m_evalCodeBlock.get())));
- if ((exception = generator->generate())) {
- m_evalCodeBlock.clear();
- evalNode->destroyData();
- return exception;
+EvalExecutable* EvalExecutable::create(ExecState* exec, const SourceCode& source, bool isInStrictContext, ThisTDZMode thisTDZMode)
+{
+ JSGlobalObject* globalObject = exec->lexicalGlobalObject();
+ if (!globalObject->evalEnabled()) {
+ exec->vm().throwException(exec, createEvalError(exec, globalObject->evalDisabledErrorMessage()));
+ return 0;
}
- evalNode->destroyData();
+ EvalExecutable* executable = new (NotNull, allocateCell<EvalExecutable>(*exec->heap())) EvalExecutable(exec, source, isInStrictContext);
+ executable->finishCreation(exec->vm());
-#if ENABLE(JIT)
- if (exec->globalData().canUseJIT()) {
- m_jitCodeForCall = JIT::compile(scopeChainNode->globalData, m_evalCodeBlock.get());
-#if !ENABLE(OPCODE_SAMPLING)
- if (!BytecodeGenerator::dumpsGeneratedCode())
- m_evalCodeBlock->discardBytecode();
-#endif
- }
-#endif
+ UnlinkedEvalCodeBlock* unlinkedEvalCode = globalObject->createEvalCodeBlock(exec, executable, thisTDZMode);
+ if (!unlinkedEvalCode)
+ return 0;
-#if ENABLE(JIT)
-#if ENABLE(INTERPRETER)
- if (!m_jitCodeForCall)
- Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
- else
-#endif
- Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock) + m_jitCodeForCall.size());
-#else
- Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
-#endif
+ executable->m_unlinkedEvalCodeBlock.set(exec->vm(), executable, unlinkedEvalCode);
- return 0;
+ return executable;
}
-void EvalExecutable::visitChildren(SlotVisitor& visitor)
+EvalExecutable::EvalExecutable(ExecState* exec, const SourceCode& source, bool inStrictContext)
+ : ScriptExecutable(exec->vm().evalExecutableStructure.get(), exec->vm(), source, inStrictContext)
{
- ASSERT_GC_OBJECT_INHERITS(this, &s_info);
- COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
- ASSERT(structure()->typeInfo().overridesVisitChildren());
- ScriptExecutable::visitChildren(visitor);
- if (m_evalCodeBlock)
- m_evalCodeBlock->visitAggregate(visitor);
}
-void EvalExecutable::unlinkCalls()
+void EvalExecutable::destroy(JSCell* cell)
{
-#if ENABLE(JIT)
- if (!m_jitCodeForCall)
- return;
- ASSERT(m_evalCodeBlock);
- m_evalCodeBlock->unlinkCalls();
-#endif
+ static_cast<EvalExecutable*>(cell)->EvalExecutable::~EvalExecutable();
}
-JSObject* ProgramExecutable::checkSyntax(ExecState* exec)
+const ClassInfo ProgramExecutable::s_info = { "ProgramExecutable", &ScriptExecutable::s_info, 0, CREATE_METHOD_TABLE(ProgramExecutable) };
+
+ProgramExecutable::ProgramExecutable(ExecState* exec, const SourceCode& source)
+ : ScriptExecutable(exec->vm().programExecutableStructure.get(), exec->vm(), source, false)
{
- JSObject* exception = 0;
- JSGlobalData* globalData = &exec->globalData();
- JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
- RefPtr<ProgramNode> programNode = globalData->parser->parse<ProgramNode>(lexicalGlobalObject, lexicalGlobalObject->debugger(), exec, m_source, 0, JSParseNormal, &exception);
- if (programNode)
- return 0;
- ASSERT(exception);
- return exception;
+ m_typeProfilingStartOffset = 0;
+ m_typeProfilingEndOffset = source.length() - 1;
+ if (exec->vm().typeProfiler() || exec->vm().controlFlowProfiler())
+ exec->vm().functionHasExecutedCache()->insertUnexecutedRange(sourceID(), m_typeProfilingStartOffset, m_typeProfilingEndOffset);
}
-JSObject* ProgramExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode)
+void ProgramExecutable::destroy(JSCell* cell)
{
- ASSERT(!m_programCodeBlock);
+ static_cast<ProgramExecutable*>(cell)->ProgramExecutable::~ProgramExecutable();
+}
- JSObject* exception = 0;
- JSGlobalData* globalData = &exec->globalData();
- JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
- RefPtr<ProgramNode> programNode = globalData->parser->parse<ProgramNode>(lexicalGlobalObject, lexicalGlobalObject->debugger(), exec, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, &exception);
- if (!programNode) {
- ASSERT(exception);
- return exception;
- }
- recordParse(programNode->features(), programNode->hasCapturedVariables(), programNode->lineNo(), programNode->lastLine());
+const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, 0, CREATE_METHOD_TABLE(FunctionExecutable) };
- JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
-
- m_programCodeBlock = adoptPtr(new ProgramCodeBlock(this, GlobalCode, globalObject, source().provider()));
- OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(programNode.get(), scopeChainNode, &globalObject->symbolTable(), m_programCodeBlock.get())));
- if ((exception = generator->generate())) {
- m_programCodeBlock.clear();
- programNode->destroyData();
- return exception;
- }
+FunctionExecutable::FunctionExecutable(VM& vm, const SourceCode& source,
+ UnlinkedFunctionExecutable* unlinkedExecutable, unsigned firstLine,
+ unsigned lastLine, unsigned startColumn, unsigned endColumn)
+ : ScriptExecutable(vm.functionExecutableStructure.get(), vm, source, unlinkedExecutable->isInStrictContext())
+ , m_unlinkedExecutable(vm, this, unlinkedExecutable)
+{
+ RELEASE_ASSERT(!source.isNull());
+ ASSERT(source.length());
+ m_firstLine = firstLine;
+ m_lastLine = lastLine;
+ ASSERT(startColumn != UINT_MAX);
+ ASSERT(endColumn != UINT_MAX);
+ m_startColumn = startColumn;
+ m_endColumn = endColumn;
+ m_parametersStartOffset = unlinkedExecutable->parametersStartOffset();
+ m_typeProfilingStartOffset = unlinkedExecutable->typeProfilingStartOffset();
+ m_typeProfilingEndOffset = unlinkedExecutable->typeProfilingEndOffset();
+}
+
+void FunctionExecutable::finishCreation(VM& vm)
+{
+ Base::finishCreation(vm);
+ m_singletonFunction.set(vm, this, InferredValue::create(vm));
+}
- programNode->destroyData();
+void FunctionExecutable::destroy(JSCell* cell)
+{
+ static_cast<FunctionExecutable*>(cell)->FunctionExecutable::~FunctionExecutable();
+}
-#if ENABLE(JIT)
- if (exec->globalData().canUseJIT()) {
- m_jitCodeForCall = JIT::compile(scopeChainNode->globalData, m_programCodeBlock.get());
-#if !ENABLE(OPCODE_SAMPLING)
- if (!BytecodeGenerator::dumpsGeneratedCode())
- m_programCodeBlock->discardBytecode();
-#endif
+inline const char* samplingDescription(JITCode::JITType jitType)
+{
+ switch (jitType) {
+ case JITCode::InterpreterThunk:
+ return "Interpreter Compilation (TOTAL)";
+ case JITCode::BaselineJIT:
+ return "Baseline Compilation (TOTAL)";
+ case JITCode::DFGJIT:
+ return "DFG Compilation (TOTAL)";
+ case JITCode::FTLJIT:
+ return "FTL Compilation (TOTAL)";
+ default:
+ RELEASE_ASSERT_NOT_REACHED();
+ return 0;
}
-#endif
-
-#if ENABLE(JIT)
-#if ENABLE(INTERPRETER)
- if (!m_jitCodeForCall)
- Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
- else
-#endif
- Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock) + m_jitCodeForCall.size());
-#else
- Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
-#endif
+}
- return 0;
+void EvalExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
+{
+ EvalExecutable* thisObject = jsCast<EvalExecutable*>(cell);
+ ASSERT_GC_OBJECT_INHERITS(thisObject, info());
+ ScriptExecutable::visitChildren(thisObject, visitor);
+ if (thisObject->m_evalCodeBlock)
+ thisObject->m_evalCodeBlock->visitAggregate(visitor);
+ visitor.append(&thisObject->m_unlinkedEvalCodeBlock);
}
-void ProgramExecutable::unlinkCalls()
+void EvalExecutable::unlinkCalls()
{
#if ENABLE(JIT)
if (!m_jitCodeForCall)
return;
- ASSERT(m_programCodeBlock);
- m_programCodeBlock->unlinkCalls();
+ RELEASE_ASSERT(m_evalCodeBlock);
+ m_evalCodeBlock->unlinkCalls();
#endif
}
-#if ENABLE(JIT)
-static bool tryDFGCompile(JSGlobalData* globalData, CodeBlock* codeBlock, JITCode& jitCode, MacroAssemblerCodePtr& jitCodeWithArityCheck)
+void EvalExecutable::clearCode()
{
-#if ENABLE(DFG_JIT)
-#if ENABLE(DFG_JIT_RESTRICTIONS)
- // FIXME: No flow control yet supported, don't bother scanning the bytecode if there are any jump targets.
- // FIXME: temporarily disable property accesses until we fix regressions.
- if (codeBlock->numberOfJumpTargets() || codeBlock->numberOfStructureStubInfos())
- return false;
-#endif
-
- DFG::Graph dfg(codeBlock->m_numParameters, codeBlock->m_numVars);
- if (!parse(dfg, globalData, codeBlock))
- return false;
+ m_evalCodeBlock = nullptr;
+ m_unlinkedEvalCodeBlock.clear();
+ Base::clearCode();
+}
- DFG::JITCompiler dataFlowJIT(globalData, dfg, codeBlock);
- dataFlowJIT.compileFunction(jitCode, jitCodeWithArityCheck);
- return true;
-#else
- UNUSED_PARAM(globalData);
- UNUSED_PARAM(codeBlock);
- UNUSED_PARAM(jitCode);
- UNUSED_PARAM(jitCodeWithArityCheck);
- return false;
-#endif
+JSObject* ProgramExecutable::checkSyntax(ExecState* exec)
+{
+ ParserError error;
+ VM* vm = &exec->vm();
+ JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
+ std::unique_ptr<ProgramNode> programNode = parse<ProgramNode>(
+ vm, m_source, 0, Identifier(), JSParserBuiltinMode::NotBuiltin,
+ JSParserStrictMode::NotStrict, JSParserCodeType::Program, error);
+ if (programNode)
+ return 0;
+ ASSERT(error.isValid());
+ return error.toErrorObject(lexicalGlobalObject, m_source);
}
-#endif
-void ProgramExecutable::visitChildren(SlotVisitor& visitor)
+void ProgramExecutable::unlinkCalls()
{
- ASSERT_GC_OBJECT_INHERITS(this, &s_info);
- COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
- ASSERT(structure()->typeInfo().overridesVisitChildren());
- ScriptExecutable::visitChildren(visitor);
- if (m_programCodeBlock)
- m_programCodeBlock->visitAggregate(visitor);
+#if ENABLE(JIT)
+ if (!m_jitCodeForCall)
+ return;
+ RELEASE_ASSERT(m_programCodeBlock);
+ m_programCodeBlock->unlinkCalls();
+#endif
}
-JSObject* FunctionExecutable::compileForCallInternal(ExecState* exec, ScopeChainNode* scopeChainNode)
+JSObject* ProgramExecutable::initializeGlobalProperties(VM& vm, CallFrame* callFrame, JSScope* scope)
{
+ RELEASE_ASSERT(scope);
+ JSGlobalObject* globalObject = scope->globalObject();
+ RELEASE_ASSERT(globalObject);
+ ASSERT(&globalObject->vm() == &vm);
+
JSObject* exception = 0;
- JSGlobalData* globalData = scopeChainNode->globalData;
- RefPtr<FunctionBodyNode> body = globalData->parser->parse<FunctionBodyNode>(exec->lexicalGlobalObject(), 0, 0, m_source, m_parameters.get(), isStrictMode() ? JSParseStrict : JSParseNormal, &exception);
- if (!body) {
- ASSERT(exception);
+ UnlinkedProgramCodeBlock* unlinkedCodeBlock = globalObject->createProgramCodeBlock(callFrame, this, &exception);
+ if (exception)
return exception;
- }
- if (m_forceUsesArguments)
- body->setUsesArguments();
- body->finishParsing(m_parameters, m_name);
- recordParse(body->features(), body->hasCapturedVariables(), body->lineNo(), body->lastLine());
-
- JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
-
- ASSERT(!m_codeBlockForCall);
- m_codeBlockForCall = adoptPtr(new FunctionCodeBlock(this, FunctionCode, globalObject, source().provider(), source().startOffset(), false));
- OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(body.get(), scopeChainNode, m_codeBlockForCall->symbolTable(), m_codeBlockForCall.get())));
- if ((exception = generator->generate())) {
- m_codeBlockForCall.clear();
- body->destroyData();
- return exception;
- }
- m_numParametersForCall = m_codeBlockForCall->m_numParameters;
- ASSERT(m_numParametersForCall);
- m_numCapturedVariables = m_codeBlockForCall->m_numCapturedVars;
- m_symbolTable = m_codeBlockForCall->sharedSymbolTable();
+ m_unlinkedProgramCodeBlock.set(vm, this, unlinkedCodeBlock);
- body->destroyData();
+ BatchedTransitionOptimizer optimizer(vm, globalObject);
-#if ENABLE(JIT)
- if (exec->globalData().canUseJIT()) {
- bool dfgCompiled = tryDFGCompile(&exec->globalData(), m_codeBlockForCall.get(), m_jitCodeForCall, m_jitCodeForCallWithArityCheck);
- if (!dfgCompiled)
- m_jitCodeForCall = JIT::compile(scopeChainNode->globalData, m_codeBlockForCall.get(), &m_jitCodeForCallWithArityCheck);
-
-#if !ENABLE(OPCODE_SAMPLING)
- if (!BytecodeGenerator::dumpsGeneratedCode())
- m_codeBlockForCall->discardBytecode();
-#endif
- }
-#endif
+ const UnlinkedProgramCodeBlock::VariableDeclations& variableDeclarations = unlinkedCodeBlock->variableDeclarations();
-#if ENABLE(JIT)
-#if ENABLE(INTERPRETER)
- if (!m_jitCodeForCall)
- Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
- else
-#endif
- Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall) + m_jitCodeForCall.size());
-#else
- Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
-#endif
+ for (size_t i = 0, numberOfFunctions = unlinkedCodeBlock->numberOfFunctionDecls(); i < numberOfFunctions; ++i) {
+ UnlinkedFunctionExecutable* unlinkedFunctionExecutable = unlinkedCodeBlock->functionDecl(i);
+ ASSERT(!unlinkedFunctionExecutable->name().isEmpty());
+ globalObject->addFunction(callFrame, unlinkedFunctionExecutable->name());
+ if (vm.typeProfiler() || vm.controlFlowProfiler()) {
+ vm.functionHasExecutedCache()->insertUnexecutedRange(sourceID(),
+ unlinkedFunctionExecutable->typeProfilingStartOffset(),
+ unlinkedFunctionExecutable->typeProfilingEndOffset());
+ }
+ }
+ for (size_t i = 0; i < variableDeclarations.size(); ++i) {
+ if (variableDeclarations[i].second & DeclarationStacks::IsConstant)
+ globalObject->addConst(callFrame, variableDeclarations[i].first);
+ else
+ globalObject->addVar(callFrame, variableDeclarations[i].first);
+ }
return 0;
}
-JSObject* FunctionExecutable::compileForConstructInternal(ExecState* exec, ScopeChainNode* scopeChainNode)
+void ProgramExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
- JSObject* exception = 0;
- JSGlobalData* globalData = scopeChainNode->globalData;
- RefPtr<FunctionBodyNode> body = globalData->parser->parse<FunctionBodyNode>(exec->lexicalGlobalObject(), 0, 0, m_source, m_parameters.get(), isStrictMode() ? JSParseStrict : JSParseNormal, &exception);
- if (!body) {
- ASSERT(exception);
- return exception;
- }
- if (m_forceUsesArguments)
- body->setUsesArguments();
- body->finishParsing(m_parameters, m_name);
- recordParse(body->features(), body->hasCapturedVariables(), body->lineNo(), body->lastLine());
-
- JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
-
- ASSERT(!m_codeBlockForConstruct);
- m_codeBlockForConstruct = adoptPtr(new FunctionCodeBlock(this, FunctionCode, globalObject, source().provider(), source().startOffset(), true));
- OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(body.get(), scopeChainNode, m_codeBlockForConstruct->symbolTable(), m_codeBlockForConstruct.get())));
- if ((exception = generator->generate())) {
- m_codeBlockForConstruct.clear();
- body->destroyData();
- return exception;
- }
-
- m_numParametersForConstruct = m_codeBlockForConstruct->m_numParameters;
- ASSERT(m_numParametersForConstruct);
- m_numCapturedVariables = m_codeBlockForConstruct->m_numCapturedVars;
- m_symbolTable = m_codeBlockForConstruct->sharedSymbolTable();
+ ProgramExecutable* thisObject = jsCast<ProgramExecutable*>(cell);
+ ASSERT_GC_OBJECT_INHERITS(thisObject, info());
+ ScriptExecutable::visitChildren(thisObject, visitor);
+ visitor.append(&thisObject->m_unlinkedProgramCodeBlock);
+ if (thisObject->m_programCodeBlock)
+ thisObject->m_programCodeBlock->visitAggregate(visitor);
+}
- body->destroyData();
+void ProgramExecutable::clearCode()
+{
+ m_programCodeBlock = nullptr;
+ m_unlinkedProgramCodeBlock.clear();
+ Base::clearCode();
+}
-#if ENABLE(JIT)
- if (exec->globalData().canUseJIT()) {
- m_jitCodeForConstruct = JIT::compile(scopeChainNode->globalData, m_codeBlockForConstruct.get(), &m_jitCodeForConstructWithArityCheck);
-#if !ENABLE(OPCODE_SAMPLING)
- if (!BytecodeGenerator::dumpsGeneratedCode())
- m_codeBlockForConstruct->discardBytecode();
-#endif
+FunctionCodeBlock* FunctionExecutable::baselineCodeBlockFor(CodeSpecializationKind kind)
+{
+ FunctionCodeBlock* result;
+ if (kind == CodeForCall)
+ result = m_codeBlockForCall.get();
+ else {
+ RELEASE_ASSERT(kind == CodeForConstruct);
+ result = m_codeBlockForConstruct.get();
}
-#endif
-
-#if ENABLE(JIT)
-#if ENABLE(INTERPRETER)
- if (!m_jitCodeForConstruct)
- Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
- else
-#endif
- Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct) + m_jitCodeForConstruct.size());
-#else
- Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
-#endif
+ if (!result)
+ return 0;
+ return static_cast<FunctionCodeBlock*>(result->baselineAlternative());
+}
- return 0;
+void FunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
+{
+ FunctionExecutable* thisObject = jsCast<FunctionExecutable*>(cell);
+ ASSERT_GC_OBJECT_INHERITS(thisObject, info());
+ ScriptExecutable::visitChildren(thisObject, visitor);
+ if (thisObject->m_codeBlockForCall)
+ thisObject->m_codeBlockForCall->visitAggregate(visitor);
+ if (thisObject->m_codeBlockForConstruct)
+ thisObject->m_codeBlockForConstruct->visitAggregate(visitor);
+ visitor.append(&thisObject->m_unlinkedExecutable);
+ visitor.append(&thisObject->m_singletonFunction);
}
-void FunctionExecutable::visitChildren(SlotVisitor& visitor)
+SymbolTable* FunctionExecutable::symbolTable(CodeSpecializationKind kind)
{
- ASSERT_GC_OBJECT_INHERITS(this, &s_info);
- COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
- ASSERT(structure()->typeInfo().overridesVisitChildren());
- ScriptExecutable::visitChildren(visitor);
- if (m_codeBlockForCall)
- m_codeBlockForCall->visitAggregate(visitor);
- if (m_codeBlockForConstruct)
- m_codeBlockForConstruct->visitAggregate(visitor);
+ return codeBlockFor(kind)->symbolTable();
}
-void FunctionExecutable::discardCode()
+void FunctionExecutable::clearUnlinkedCodeForRecompilation()
{
-#if ENABLE(JIT)
- // These first two checks are to handle the rare case where
- // we are trying to evict code for a function during its
- // codegen.
- if (!m_jitCodeForCall && m_codeBlockForCall)
- return;
- if (!m_jitCodeForConstruct && m_codeBlockForConstruct)
- return;
- m_jitCodeForCall = JITCode();
- m_jitCodeForConstruct = JITCode();
- m_jitCodeForCallWithArityCheck = MacroAssemblerCodePtr();
- m_jitCodeForConstructWithArityCheck = MacroAssemblerCodePtr();
-#endif
- if (m_codeBlockForCall)
- m_codeBlockForCall->clearEvalCache();
- m_codeBlockForCall.clear();
- if (m_codeBlockForConstruct)
- m_codeBlockForConstruct->clearEvalCache();
- m_codeBlockForConstruct.clear();
- m_numParametersForCall = NUM_PARAMETERS_NOT_COMPILED;
- m_numParametersForConstruct = NUM_PARAMETERS_NOT_COMPILED;
+ m_unlinkedExecutable->clearCodeForRecompilation();
+}
+void FunctionExecutable::clearCode()
+{
+ m_codeBlockForCall = nullptr;
+ m_codeBlockForConstruct = nullptr;
+ Base::clearCode();
}
void FunctionExecutable::unlinkCalls()
{
#if ENABLE(JIT)
if (!!m_jitCodeForCall) {
- ASSERT(m_codeBlockForCall);
+ RELEASE_ASSERT(m_codeBlockForCall);
m_codeBlockForCall->unlinkCalls();
}
if (!!m_jitCodeForConstruct) {
- ASSERT(m_codeBlockForConstruct);
+ RELEASE_ASSERT(m_codeBlockForConstruct);
m_codeBlockForConstruct->unlinkCalls();
}
#endif
}
-FunctionExecutable* FunctionExecutable::fromGlobalCode(const Identifier& functionName, ExecState* exec, Debugger* debugger, const SourceCode& source, JSObject** exception)
+FunctionExecutable* FunctionExecutable::fromGlobalCode(
+ const Identifier& name, ExecState& exec, const SourceCode& source,
+ JSObject*& exception, int overrideLineNumber)
{
- JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
- RefPtr<ProgramNode> program = exec->globalData().parser->parse<ProgramNode>(lexicalGlobalObject, debugger, exec, source, 0, JSParseNormal, exception);
- if (!program) {
- ASSERT(*exception);
- return 0;
+ UnlinkedFunctionExecutable* unlinkedExecutable =
+ UnlinkedFunctionExecutable::fromGlobalCode(
+ name, exec, source, exception, overrideLineNumber);
+ if (!unlinkedExecutable)
+ return nullptr;
+
+ return unlinkedExecutable->link(exec.vm(), source, overrideLineNumber);
+}
+
+void ExecutableBase::dump(PrintStream& out) const
+{
+ ExecutableBase* realThis = const_cast<ExecutableBase*>(this);
+
+ if (classInfo() == NativeExecutable::info()) {
+ NativeExecutable* native = jsCast<NativeExecutable*>(realThis);
+ out.print("NativeExecutable:", RawPointer(bitwise_cast<void*>(native->function())), "/", RawPointer(bitwise_cast<void*>(native->constructor())));
+ return;
+ }
+
+ if (classInfo() == EvalExecutable::info()) {
+ EvalExecutable* eval = jsCast<EvalExecutable*>(realThis);
+ if (CodeBlock* codeBlock = eval->codeBlock())
+ out.print(*codeBlock);
+ else
+ out.print("EvalExecutable w/o CodeBlock");
+ return;
+ }
+
+ if (classInfo() == ProgramExecutable::info()) {
+ ProgramExecutable* eval = jsCast<ProgramExecutable*>(realThis);
+ if (CodeBlock* codeBlock = eval->codeBlock())
+ out.print(*codeBlock);
+ else
+ out.print("ProgramExecutable w/o CodeBlock");
+ return;
+ }
+
+ FunctionExecutable* function = jsCast<FunctionExecutable*>(realThis);
+ if (!function->eitherCodeBlock())
+ out.print("FunctionExecutable w/o CodeBlock");
+ else {
+ CommaPrinter comma("/");
+ if (function->codeBlockForCall())
+ out.print(comma, *function->codeBlockForCall());
+ if (function->codeBlockForConstruct())
+ out.print(comma, *function->codeBlockForConstruct());
}
+}
- // Uses of this function that would not result in a single function expression are invalid.
- StatementNode* exprStatement = program->singleStatement();
- ASSERT(exprStatement);
- ASSERT(exprStatement->isExprStatement());
- ExpressionNode* funcExpr = static_cast<ExprStatementNode*>(exprStatement)->expr();
- ASSERT(funcExpr);
- ASSERT(funcExpr->isFuncExprNode());
- FunctionBodyNode* body = static_cast<FuncExprNode*>(funcExpr)->body();
- ASSERT(body);
+CodeBlockHash ExecutableBase::hashFor(CodeSpecializationKind kind) const
+{
+ if (this->classInfo() == NativeExecutable::info())
+ return jsCast<const NativeExecutable*>(this)->hashFor(kind);
+
+ return jsCast<const ScriptExecutable*>(this)->hashFor(kind);
+}
- return FunctionExecutable::create(&exec->globalData(), functionName, body->source(), body->usesArguments(), body->parameters(), body->isStrictMode(), body->lineNo(), body->lastLine());
+CodeBlockHash NativeExecutable::hashFor(CodeSpecializationKind kind) const
+{
+ if (kind == CodeForCall)
+ return CodeBlockHash(static_cast<unsigned>(bitwise_cast<size_t>(m_function)));
+
+ RELEASE_ASSERT(kind == CodeForConstruct);
+ return CodeBlockHash(static_cast<unsigned>(bitwise_cast<size_t>(m_constructor)));
}
-UString FunctionExecutable::paramString() const
+CodeBlockHash ScriptExecutable::hashFor(CodeSpecializationKind kind) const
{
- FunctionParameters& parameters = *m_parameters;
- UStringBuilder builder;
- for (size_t pos = 0; pos < parameters.size(); ++pos) {
- if (!builder.isEmpty())
- builder.append(", ");
- builder.append(parameters[pos].ustring());
- }
- return builder.toUString();
+ return CodeBlockHash(source(), kind);
}
}