X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/f9bf01c6616d5ddcf65b13b33cedf9e387ff7a63..ed1e77d3adeb83d26fd1dfb16dd84cabdcefd250:/runtime/Executable.cpp?ds=inline diff --git a/runtime/Executable.cpp b/runtime/Executable.cpp index bc18cc9..55240fd 100644 --- a/runtime/Executable.cpp +++ b/runtime/Executable.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 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 @@ -26,255 +26,659 @@ #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 "StringBuilder.h" -#include "Vector.h" +#include "ProfilerDatabase.h" +#include "TypeProfiler.h" +#include +#include +#include namespace JSC { +const ClassInfo ExecutableBase::s_info = { "Executable", 0, 0, CREATE_METHOD_TABLE(ExecutableBase) }; + +void ExecutableBase::destroy(JSCell* cell) +{ + static_cast(cell)->ExecutableBase::~ExecutableBase(); +} + +void ExecutableBase::clearCode() +{ #if ENABLE(JIT) -NativeExecutable::~NativeExecutable() + 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; +} + +#if ENABLE(DFG_JIT) +Intrinsic ExecutableBase::intrinsic() const { + if (const NativeExecutable* nativeExecutable = jsDynamicCast(this)) + return nativeExecutable->intrinsic(); + return NoIntrinsic; +} +#else +Intrinsic ExecutableBase::intrinsic() const +{ + return NoIntrinsic; } #endif -VPtrHackExecutable::~VPtrHackExecutable() +const ClassInfo NativeExecutable::s_info = { "NativeExecutable", &ExecutableBase::s_info, 0, CREATE_METHOD_TABLE(NativeExecutable) }; + +void NativeExecutable::destroy(JSCell* cell) { + static_cast(cell)->NativeExecutable::~NativeExecutable(); } -EvalExecutable::~EvalExecutable() +#if ENABLE(DFG_JIT) +Intrinsic NativeExecutable::intrinsic() const { - delete m_evalCodeBlock; + return m_intrinsic; } +#endif -ProgramExecutable::~ProgramExecutable() +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) { - delete m_programCodeBlock; } -FunctionExecutable::~FunctionExecutable() +void ScriptExecutable::destroy(JSCell* cell) { - delete m_codeBlock; + static_cast(cell)->ScriptExecutable::~ScriptExecutable(); } -JSObject* EvalExecutable::compile(ExecState* exec, ScopeChainNode* scopeChainNode) +void ScriptExecutable::installCode(CodeBlock* genericCodeBlock) { - int errLine; - UString errMsg; - RefPtr evalNode = exec->globalData().parser->parse(&exec->globalData(), exec->lexicalGlobalObject()->debugger(), exec, m_source, &errLine, &errMsg); - if (!evalNode) - return Error::create(exec, SyntaxError, errMsg, errLine, m_source.provider()->asID(), m_source.provider()->url()); - recordParse(evalNode->features(), evalNode->lineNo(), evalNode->lastLine()); + 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 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(this); + ProgramCodeBlock* codeBlock = static_cast(genericCodeBlock); + + ASSERT(kind == CodeForCall); + + oldCodeBlock = executable->m_programCodeBlock; + executable->m_programCodeBlock = codeBlock; + break; + } + + case EvalCode: { + EvalExecutable* executable = jsCast(this); + EvalCodeBlock* codeBlock = static_cast(genericCodeBlock); + + ASSERT(kind == CodeForCall); + + oldCodeBlock = executable->m_evalCodeBlock; + executable->m_evalCodeBlock = codeBlock; + break; + } + + case FunctionCode: { + FunctionExecutable* executable = jsCast(this); + FunctionCodeBlock* codeBlock = static_cast(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); +} - ScopeChain scopeChain(scopeChainNode); - JSGlobalObject* globalObject = scopeChain.globalObject(); +RefPtr 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(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(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(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; + } - ASSERT(!m_evalCodeBlock); - m_evalCodeBlock = new EvalCodeBlock(this, globalObject, source().provider(), scopeChain.localDepth()); - OwnPtr generator(new BytecodeGenerator(evalNode.get(), globalObject->debugger(), scopeChain, m_evalCodeBlock->symbolTable(), m_evalCodeBlock)); - generator->generate(); + // 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); + } - evalNode->destroyData(); - return 0; + SourceProvider* provider = executable->source().provider(); + unsigned sourceOffset = executable->source().startOffset(); + unsigned startColumn = executable->source().startColumn(); + + return adoptRef(new FunctionCodeBlock( + executable, unlinkedCodeBlock, scope, provider, sourceOffset, startColumn)); } -JSObject* ProgramExecutable::checkSyntax(ExecState* exec) +PassRefPtr ScriptExecutable::newReplacementCodeBlockFor( + CodeSpecializationKind kind) { - int errLine; - UString errMsg; - RefPtr programNode = exec->globalData().parser->parse(&exec->globalData(), exec->lexicalGlobalObject()->debugger(), exec, m_source, &errLine, &errMsg); - if (!programNode) - return Error::create(exec, SyntaxError, errMsg, errLine, m_source.provider()->asID(), m_source.provider()->url()); - return 0; + if (classInfo() == EvalExecutable::info()) { + RELEASE_ASSERT(kind == CodeForCall); + EvalExecutable* executable = jsCast(this); + EvalCodeBlock* baseline = static_cast( + executable->m_evalCodeBlock->baselineVersion()); + RefPtr result = adoptRef(new EvalCodeBlock( + CodeBlock::CopyParsedBlock, *baseline)); + result->setAlternative(baseline); + return result; + } + + if (classInfo() == ProgramExecutable::info()) { + RELEASE_ASSERT(kind == CodeForCall); + ProgramExecutable* executable = jsCast(this); + ProgramCodeBlock* baseline = static_cast( + executable->m_programCodeBlock->baselineVersion()); + RefPtr result = adoptRef(new ProgramCodeBlock( + CodeBlock::CopyParsedBlock, *baseline)); + result->setAlternative(baseline); + return result; + } + + RELEASE_ASSERT(classInfo() == FunctionExecutable::info()); + FunctionExecutable* executable = jsCast(this); + FunctionCodeBlock* baseline = static_cast( + executable->codeBlockFor(kind)->baselineVersion()); + RefPtr result = adoptRef(new FunctionCodeBlock( + CodeBlock::CopyParsedBlock, *baseline)); + result->setAlternative(baseline); + return result; } -JSObject* ProgramExecutable::compile(ExecState* exec, ScopeChainNode* scopeChainNode) +static void setupLLInt(VM& vm, CodeBlock* codeBlock) { - int errLine; - UString errMsg; - RefPtr programNode = exec->globalData().parser->parse(&exec->globalData(), exec->lexicalGlobalObject()->debugger(), exec, m_source, &errLine, &errMsg); - if (!programNode) - return Error::create(exec, SyntaxError, errMsg, errLine, m_source.provider()->asID(), m_source.provider()->url()); - recordParse(programNode->features(), programNode->lineNo(), programNode->lastLine()); + LLInt::setEntrypoint(vm, codeBlock); +} - ScopeChain scopeChain(scopeChainNode); - JSGlobalObject* globalObject = scopeChain.globalObject(); - - ASSERT(!m_programCodeBlock); - m_programCodeBlock = new ProgramCodeBlock(this, GlobalCode, globalObject, source().provider()); - OwnPtr generator(new BytecodeGenerator(programNode.get(), globalObject->debugger(), scopeChain, &globalObject->symbolTable(), m_programCodeBlock)); - generator->generate(); +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 +} - programNode->destroyData(); +JSObject* ScriptExecutable::prepareForExecutionImpl( + ExecState* exec, JSFunction* function, JSScope* scope, CodeSpecializationKind kind) +{ + VM& vm = exec->vm(); + DeferGC deferGC(vm.heap); + + JSObject* exception = 0; + RefPtr codeBlock = newCodeBlockFor(kind, function, scope, exception); + if (!codeBlock) { + RELEASE_ASSERT(exception); + return exception; + } + + if (Options::validateBytecode()) + codeBlock->validate(); + + if (Options::useLLInt()) + setupLLInt(vm, codeBlock.get()); + else + setupJIT(vm, codeBlock.get()); + + installCode(codeBlock.get()); return 0; } -void FunctionExecutable::compile(ExecState*, ScopeChainNode* scopeChainNode) +const ClassInfo EvalExecutable::s_info = { "EvalExecutable", &ScriptExecutable::s_info, 0, CREATE_METHOD_TABLE(EvalExecutable) }; + +EvalExecutable* EvalExecutable::create(ExecState* exec, const SourceCode& source, bool isInStrictContext, ThisTDZMode thisTDZMode) { - JSGlobalData* globalData = scopeChainNode->globalData; - RefPtr body = globalData->parser->parse(globalData, 0, 0, m_source); - if (m_forceUsesArguments) - body->setUsesArguments(); - body->finishParsing(m_parameters, m_name); - recordParse(body->features(), body->lineNo(), body->lastLine()); + JSGlobalObject* globalObject = exec->lexicalGlobalObject(); + if (!globalObject->evalEnabled()) { + exec->vm().throwException(exec, createEvalError(exec, globalObject->evalDisabledErrorMessage())); + return 0; + } - ScopeChain scopeChain(scopeChainNode); - JSGlobalObject* globalObject = scopeChain.globalObject(); + EvalExecutable* executable = new (NotNull, allocateCell(*exec->heap())) EvalExecutable(exec, source, isInStrictContext); + executable->finishCreation(exec->vm()); - ASSERT(!m_codeBlock); - m_codeBlock = new FunctionCodeBlock(this, FunctionCode, source().provider(), source().startOffset()); - OwnPtr generator(new BytecodeGenerator(body.get(), globalObject->debugger(), scopeChain, m_codeBlock->symbolTable(), m_codeBlock)); - generator->generate(); - m_numParameters = m_codeBlock->m_numParameters; - ASSERT(m_numParameters); - m_numVariables = m_codeBlock->m_numVars; + UnlinkedEvalCodeBlock* unlinkedEvalCode = globalObject->createEvalCodeBlock(exec, executable, thisTDZMode); + if (!unlinkedEvalCode) + return 0; - body->destroyData(); -} + executable->m_unlinkedEvalCodeBlock.set(exec->vm(), executable, unlinkedEvalCode); -#if ENABLE(JIT) + return executable; +} -void EvalExecutable::generateJITCode(ExecState* exec, ScopeChainNode* scopeChainNode) +EvalExecutable::EvalExecutable(ExecState* exec, const SourceCode& source, bool inStrictContext) + : ScriptExecutable(exec->vm().evalExecutableStructure.get(), exec->vm(), source, inStrictContext) { - CodeBlock* codeBlock = &bytecode(exec, scopeChainNode); - m_jitCode = JIT::compile(scopeChainNode->globalData, codeBlock); - -#if !ENABLE(OPCODE_SAMPLING) - if (!BytecodeGenerator::dumpsGeneratedCode()) - codeBlock->discardBytecode(); -#endif } -void ProgramExecutable::generateJITCode(ExecState* exec, ScopeChainNode* scopeChainNode) +void EvalExecutable::destroy(JSCell* cell) { - CodeBlock* codeBlock = &bytecode(exec, scopeChainNode); - m_jitCode = JIT::compile(scopeChainNode->globalData, codeBlock); + static_cast(cell)->EvalExecutable::~EvalExecutable(); +} -#if !ENABLE(OPCODE_SAMPLING) - if (!BytecodeGenerator::dumpsGeneratedCode()) - codeBlock->discardBytecode(); -#endif +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) +{ + m_typeProfilingStartOffset = 0; + m_typeProfilingEndOffset = source.length() - 1; + if (exec->vm().typeProfiler() || exec->vm().controlFlowProfiler()) + exec->vm().functionHasExecutedCache()->insertUnexecutedRange(sourceID(), m_typeProfilingStartOffset, m_typeProfilingEndOffset); } -void FunctionExecutable::generateJITCode(ExecState* exec, ScopeChainNode* scopeChainNode) +void ProgramExecutable::destroy(JSCell* cell) { - CodeBlock* codeBlock = &bytecode(exec, scopeChainNode); - m_jitCode = JIT::compile(scopeChainNode->globalData, codeBlock); + static_cast(cell)->ProgramExecutable::~ProgramExecutable(); +} -#if !ENABLE(OPCODE_SAMPLING) - if (!BytecodeGenerator::dumpsGeneratedCode()) - codeBlock->discardBytecode(); -#endif +const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, 0, CREATE_METHOD_TABLE(FunctionExecutable) }; + +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(); } -#endif +void FunctionExecutable::finishCreation(VM& vm) +{ + Base::finishCreation(vm); + m_singletonFunction.set(vm, this, InferredValue::create(vm)); +} -void FunctionExecutable::markAggregate(MarkStack& markStack) +void FunctionExecutable::destroy(JSCell* cell) { - if (m_codeBlock) - m_codeBlock->markAggregate(markStack); + static_cast(cell)->FunctionExecutable::~FunctionExecutable(); } -ExceptionInfo* FunctionExecutable::reparseExceptionInfo(JSGlobalData* globalData, ScopeChainNode* scopeChainNode, CodeBlock* codeBlock) +inline const char* samplingDescription(JITCode::JITType jitType) { - RefPtr newFunctionBody = globalData->parser->parse(globalData, 0, 0, m_source); - if (m_forceUsesArguments) - newFunctionBody->setUsesArguments(); - newFunctionBody->finishParsing(m_parameters, m_name); + 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; + } +} - ScopeChain scopeChain(scopeChainNode); - JSGlobalObject* globalObject = scopeChain.globalObject(); +void EvalExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor) +{ + EvalExecutable* thisObject = jsCast(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); +} - OwnPtr newCodeBlock(new FunctionCodeBlock(this, FunctionCode, source().provider(), source().startOffset())); - globalData->functionCodeBlockBeingReparsed = newCodeBlock.get(); +void EvalExecutable::unlinkCalls() +{ +#if ENABLE(JIT) + if (!m_jitCodeForCall) + return; + RELEASE_ASSERT(m_evalCodeBlock); + m_evalCodeBlock->unlinkCalls(); +#endif +} - OwnPtr generator(new BytecodeGenerator(newFunctionBody.get(), globalObject->debugger(), scopeChain, newCodeBlock->symbolTable(), newCodeBlock.get())); - generator->setRegeneratingForExceptionInfo(static_cast(codeBlock)); - generator->generate(); +void EvalExecutable::clearCode() +{ + m_evalCodeBlock = nullptr; + m_unlinkedEvalCodeBlock.clear(); + Base::clearCode(); +} - ASSERT(newCodeBlock->instructionCount() == codeBlock->instructionCount()); +JSObject* ProgramExecutable::checkSyntax(ExecState* exec) +{ + ParserError error; + VM* vm = &exec->vm(); + JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject(); + std::unique_ptr programNode = parse( + 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); +} +void ProgramExecutable::unlinkCalls() +{ #if ENABLE(JIT) - JITCode newJITCode = JIT::compile(globalData, newCodeBlock.get()); - ASSERT(newJITCode.size() == generatedJITCode().size()); + if (!m_jitCodeForCall) + return; + RELEASE_ASSERT(m_programCodeBlock); + m_programCodeBlock->unlinkCalls(); #endif +} - globalData->functionCodeBlockBeingReparsed = 0; +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; + UnlinkedProgramCodeBlock* unlinkedCodeBlock = globalObject->createProgramCodeBlock(callFrame, this, &exception); + if (exception) + return exception; + + m_unlinkedProgramCodeBlock.set(vm, this, unlinkedCodeBlock); + + BatchedTransitionOptimizer optimizer(vm, globalObject); + + const UnlinkedProgramCodeBlock::VariableDeclations& variableDeclarations = unlinkedCodeBlock->variableDeclarations(); + + 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()); + } + } - return newCodeBlock->extractExceptionInfo(); + 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; } -ExceptionInfo* EvalExecutable::reparseExceptionInfo(JSGlobalData* globalData, ScopeChainNode* scopeChainNode, CodeBlock* codeBlock) +void ProgramExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor) { - RefPtr newEvalBody = globalData->parser->parse(globalData, 0, 0, m_source); + ProgramExecutable* thisObject = jsCast(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); +} - ScopeChain scopeChain(scopeChainNode); - JSGlobalObject* globalObject = scopeChain.globalObject(); +void ProgramExecutable::clearCode() +{ + m_programCodeBlock = nullptr; + m_unlinkedProgramCodeBlock.clear(); + Base::clearCode(); +} - OwnPtr newCodeBlock(new EvalCodeBlock(this, globalObject, source().provider(), scopeChain.localDepth())); +FunctionCodeBlock* FunctionExecutable::baselineCodeBlockFor(CodeSpecializationKind kind) +{ + FunctionCodeBlock* result; + if (kind == CodeForCall) + result = m_codeBlockForCall.get(); + else { + RELEASE_ASSERT(kind == CodeForConstruct); + result = m_codeBlockForConstruct.get(); + } + if (!result) + return 0; + return static_cast(result->baselineAlternative()); +} - OwnPtr generator(new BytecodeGenerator(newEvalBody.get(), globalObject->debugger(), scopeChain, newCodeBlock->symbolTable(), newCodeBlock.get())); - generator->setRegeneratingForExceptionInfo(static_cast(codeBlock)); - generator->generate(); +void FunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor) +{ + FunctionExecutable* thisObject = jsCast(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); +} - ASSERT(newCodeBlock->instructionCount() == codeBlock->instructionCount()); +SymbolTable* FunctionExecutable::symbolTable(CodeSpecializationKind kind) +{ + return codeBlockFor(kind)->symbolTable(); +} -#if ENABLE(JIT) - JITCode newJITCode = JIT::compile(globalData, newCodeBlock.get()); - ASSERT(newJITCode.size() == generatedJITCode().size()); -#endif +void FunctionExecutable::clearUnlinkedCodeForRecompilation() +{ + m_unlinkedExecutable->clearCodeForRecompilation(); +} - return newCodeBlock->extractExceptionInfo(); +void FunctionExecutable::clearCode() +{ + m_codeBlockForCall = nullptr; + m_codeBlockForConstruct = nullptr; + Base::clearCode(); } -void FunctionExecutable::recompile(ExecState*) +void FunctionExecutable::unlinkCalls() { - delete m_codeBlock; - m_codeBlock = 0; - m_numParameters = NUM_PARAMETERS_NOT_COMPILED; #if ENABLE(JIT) - m_jitCode = JITCode(); + if (!!m_jitCodeForCall) { + RELEASE_ASSERT(m_codeBlockForCall); + m_codeBlockForCall->unlinkCalls(); + } + if (!!m_jitCodeForConstruct) { + RELEASE_ASSERT(m_codeBlockForConstruct); + m_codeBlockForConstruct->unlinkCalls(); + } #endif } -PassRefPtr FunctionExecutable::fromGlobalCode(const Identifier& functionName, ExecState* exec, Debugger* debugger, const SourceCode& source, int* errLine, UString* errMsg) +FunctionExecutable* FunctionExecutable::fromGlobalCode( + const Identifier& name, ExecState& exec, const SourceCode& source, + JSObject*& exception, int overrideLineNumber) { - RefPtr program = exec->globalData().parser->parse(&exec->globalData(), debugger, exec, source, errLine, errMsg); - if (!program) - return 0; - - StatementNode* exprStatement = program->singleStatement(); - ASSERT(exprStatement); - ASSERT(exprStatement->isExprStatement()); - if (!exprStatement || !exprStatement->isExprStatement()) - return 0; - - ExpressionNode* funcExpr = static_cast(exprStatement)->expr(); - ASSERT(funcExpr); - ASSERT(funcExpr->isFuncExprNode()); - if (!funcExpr || !funcExpr->isFuncExprNode()) - return 0; + UnlinkedFunctionExecutable* unlinkedExecutable = + UnlinkedFunctionExecutable::fromGlobalCode( + name, exec, source, exception, overrideLineNumber); + if (!unlinkedExecutable) + return nullptr; - FunctionBodyNode* body = static_cast(funcExpr)->body(); - ASSERT(body); - return FunctionExecutable::create(&exec->globalData(), functionName, body->source(), body->usesArguments(), body->parameters(), body->lineNo(), body->lastLine()); + return unlinkedExecutable->link(exec.vm(), source, overrideLineNumber); } -UString FunctionExecutable::paramString() const +void ExecutableBase::dump(PrintStream& out) const { - FunctionParameters& parameters = *m_parameters; - StringBuilder builder; - for (size_t pos = 0; pos < parameters.size(); ++pos) { - if (!builder.isEmpty()) - builder.append(", "); - builder.append(parameters[pos].ustring()); + ExecutableBase* realThis = const_cast(this); + + if (classInfo() == NativeExecutable::info()) { + NativeExecutable* native = jsCast(realThis); + out.print("NativeExecutable:", RawPointer(bitwise_cast(native->function())), "/", RawPointer(bitwise_cast(native->constructor()))); + return; } - return builder.release(); + + if (classInfo() == EvalExecutable::info()) { + EvalExecutable* eval = jsCast(realThis); + if (CodeBlock* codeBlock = eval->codeBlock()) + out.print(*codeBlock); + else + out.print("EvalExecutable w/o CodeBlock"); + return; + } + + if (classInfo() == ProgramExecutable::info()) { + ProgramExecutable* eval = jsCast(realThis); + if (CodeBlock* codeBlock = eval->codeBlock()) + out.print(*codeBlock); + else + out.print("ProgramExecutable w/o CodeBlock"); + return; + } + + FunctionExecutable* function = jsCast(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()); + } +} + +CodeBlockHash ExecutableBase::hashFor(CodeSpecializationKind kind) const +{ + if (this->classInfo() == NativeExecutable::info()) + return jsCast(this)->hashFor(kind); + + return jsCast(this)->hashFor(kind); } -}; +CodeBlockHash NativeExecutable::hashFor(CodeSpecializationKind kind) const +{ + if (kind == CodeForCall) + return CodeBlockHash(static_cast(bitwise_cast(m_function))); + + RELEASE_ASSERT(kind == CodeForConstruct); + return CodeBlockHash(static_cast(bitwise_cast(m_constructor))); +} +CodeBlockHash ScriptExecutable::hashFor(CodeSpecializationKind kind) const +{ + return CodeBlockHash(source(), kind); +} +}