X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/6fe7ccc865dc7d7541b93c5bcaf6368d2c98a174..ed1e77d3adeb83d26fd1dfb16dd84cabdcefd250:/runtime/Executable.h?ds=sidebyside diff --git a/runtime/Executable.h b/runtime/Executable.h index 4179a88..1c7c800 100644 --- a/runtime/Executable.h +++ b/runtime/Executable.h @@ -1,5 +1,5 @@ /* - * 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 @@ -26,719 +26,685 @@ #ifndef Executable_h #define Executable_h +#include "ArityCheckMode.h" #include "CallData.h" +#include "CodeBlockHash.h" #include "CodeSpecializationKind.h" -#include "JSFunction.h" -#include "Interpreter.h" -#include "Nodes.h" +#include "CompilationResult.h" +#include "DFGPlan.h" +#include "HandlerInfo.h" +#include "InferredValue.h" +#include "JITCode.h" +#include "JSGlobalObject.h" +#include "RegisterPreservationMode.h" #include "SamplingTool.h" -#include +#include "SourceCode.h" +#include "TypeSet.h" +#include "UnlinkedCodeBlock.h" namespace JSC { - class CodeBlock; - class Debugger; - class EvalCodeBlock; - class FunctionCodeBlock; - class LLIntOffsetsExtractor; - class ProgramCodeBlock; - class ScopeChainNode; +class CodeBlock; +class Debugger; +class EvalCodeBlock; +class FunctionCodeBlock; +class LLIntOffsetsExtractor; +class ProgramCodeBlock; +class JSScope; - enum CompilationKind { FirstCompilation, OptimizingCompilation }; +enum CompilationKind { FirstCompilation, OptimizingCompilation }; + +inline bool isCall(CodeSpecializationKind kind) +{ + if (kind == CodeForCall) + return true; + ASSERT(kind == CodeForConstruct); + return false; +} + +class ExecutableBase : public JSCell { + friend class JIT; - inline bool isCall(CodeSpecializationKind kind) +protected: + static const int NUM_PARAMETERS_IS_HOST = 0; + static const int NUM_PARAMETERS_NOT_COMPILED = -1; + + ExecutableBase(VM& vm, Structure* structure, int numParameters) + : JSCell(vm, structure) + , m_numParametersForCall(numParameters) + , m_numParametersForConstruct(numParameters) { - if (kind == CodeForCall) - return true; - ASSERT(kind == CodeForConstruct); - return false; } - class ExecutableBase : public JSCell { - friend class JIT; + void finishCreation(VM& vm) + { + Base::finishCreation(vm); + } - protected: - static const int NUM_PARAMETERS_IS_HOST = 0; - static const int NUM_PARAMETERS_NOT_COMPILED = -1; +public: + typedef JSCell Base; + static const unsigned StructureFlags = Base::StructureFlags; - ExecutableBase(JSGlobalData& globalData, Structure* structure, int numParameters) - : JSCell(globalData, structure) - , m_numParametersForCall(numParameters) - , m_numParametersForConstruct(numParameters) - { - } + static const bool needsDestruction = true; + static void destroy(JSCell*); + + CodeBlockHash hashFor(CodeSpecializationKind) const; - void finishCreation(JSGlobalData& globalData) - { - Base::finishCreation(globalData); - } + bool isEvalExecutable() + { + return type() == EvalExecutableType; + } + bool isFunctionExecutable() + { + return type() == FunctionExecutableType; + } + bool isProgramExecutable() + { + return type() == ProgramExecutableType; + } - public: - typedef JSCell Base; + bool isHostFunction() const + { + ASSERT((m_numParametersForCall == NUM_PARAMETERS_IS_HOST) == (m_numParametersForConstruct == NUM_PARAMETERS_IS_HOST)); + return m_numParametersForCall == NUM_PARAMETERS_IS_HOST; + } -#if ENABLE(JIT) - static void destroy(JSCell*); -#endif + static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) { return Structure::create(vm, globalObject, proto, TypeInfo(CellType, StructureFlags), info()); } + + void clearCode(); - bool isHostFunction() const - { - ASSERT((m_numParametersForCall == NUM_PARAMETERS_IS_HOST) == (m_numParametersForConstruct == NUM_PARAMETERS_IS_HOST)); - return m_numParametersForCall == NUM_PARAMETERS_IS_HOST; - } + DECLARE_EXPORT_INFO; - static Structure* createStructure(JSGlobalData& globalData, JSGlobalObject* globalObject, JSValue proto) { return Structure::create(globalData, globalObject, proto, TypeInfo(CompoundType, StructureFlags), &s_info); } - - static JS_EXPORTDATA const ClassInfo s_info; - - protected: - static const unsigned StructureFlags = 0; - int m_numParametersForCall; - int m_numParametersForConstruct; - -#if ENABLE(JIT) - public: - JITCode& generatedJITCodeForCall() - { - ASSERT(m_jitCodeForCall); - return m_jitCodeForCall; - } +protected: + int m_numParametersForCall; + int m_numParametersForConstruct; - JITCode& generatedJITCodeForConstruct() - { - ASSERT(m_jitCodeForConstruct); - return m_jitCodeForConstruct; - } - - JITCode& generatedJITCodeFor(CodeSpecializationKind kind) - { - if (kind == CodeForCall) - return generatedJITCodeForCall(); - ASSERT(kind == CodeForConstruct); - return generatedJITCodeForConstruct(); - } +public: + static void clearCodeVirtual(ExecutableBase*); - MacroAssemblerCodePtr generatedJITCodeForCallWithArityCheck() - { - ASSERT(m_jitCodeForCall); - ASSERT(m_jitCodeForCallWithArityCheck); - return m_jitCodeForCallWithArityCheck; - } + PassRefPtr generatedJITCodeForCall() + { + ASSERT(m_jitCodeForCall); + return m_jitCodeForCall; + } - MacroAssemblerCodePtr generatedJITCodeForConstructWithArityCheck() - { - ASSERT(m_jitCodeForConstruct); - ASSERT(m_jitCodeForConstructWithArityCheck); - return m_jitCodeForConstructWithArityCheck; - } - - MacroAssemblerCodePtr generatedJITCodeWithArityCheckFor(CodeSpecializationKind kind) - { - if (kind == CodeForCall) - return generatedJITCodeForCallWithArityCheck(); - ASSERT(kind == CodeForConstruct); - return generatedJITCodeForConstructWithArityCheck(); - } + PassRefPtr generatedJITCodeForConstruct() + { + ASSERT(m_jitCodeForConstruct); + return m_jitCodeForConstruct; + } - bool hasJITCodeForCall() const - { - return m_numParametersForCall >= 0; + PassRefPtr generatedJITCodeFor(CodeSpecializationKind kind) + { + if (kind == CodeForCall) + return generatedJITCodeForCall(); + ASSERT(kind == CodeForConstruct); + return generatedJITCodeForConstruct(); + } + + MacroAssemblerCodePtr entrypointFor( + VM& vm, CodeSpecializationKind kind, ArityCheckMode arity, RegisterPreservationMode registers) + { + // Check if we have a cached result. We only have it for arity check because we use the + // no-arity entrypoint in non-virtual calls, which will "cache" this value directly in + // machine code. + if (arity == MustCheckArity) { + switch (kind) { + case CodeForCall: + switch (registers) { + case RegisterPreservationNotRequired: + if (MacroAssemblerCodePtr result = m_jitCodeForCallWithArityCheck) + return result; + break; + case MustPreserveRegisters: + if (MacroAssemblerCodePtr result = m_jitCodeForCallWithArityCheckAndPreserveRegs) + return result; + break; + } + break; + case CodeForConstruct: + switch (registers) { + case RegisterPreservationNotRequired: + if (MacroAssemblerCodePtr result = m_jitCodeForConstructWithArityCheck) + return result; + break; + case MustPreserveRegisters: + if (MacroAssemblerCodePtr result = m_jitCodeForConstructWithArityCheckAndPreserveRegs) + return result; + break; + } + break; + } } - - bool hasJITCodeForConstruct() const - { - return m_numParametersForConstruct >= 0; + MacroAssemblerCodePtr result = + generatedJITCodeFor(kind)->addressForCall(vm, this, arity, registers); + if (arity == MustCheckArity) { + // Cache the result; this is necessary for the JIT's virtual call optimizations. + switch (kind) { + case CodeForCall: + switch (registers) { + case RegisterPreservationNotRequired: + m_jitCodeForCallWithArityCheck = result; + break; + case MustPreserveRegisters: + m_jitCodeForCallWithArityCheckAndPreserveRegs = result; + break; + } + break; + case CodeForConstruct: + switch (registers) { + case RegisterPreservationNotRequired: + m_jitCodeForConstructWithArityCheck = result; + break; + case MustPreserveRegisters: + m_jitCodeForConstructWithArityCheckAndPreserveRegs = result; + break; + } + break; + } } - - bool hasJITCodeFor(CodeSpecializationKind kind) const - { - if (kind == CodeForCall) - return hasJITCodeForCall(); - ASSERT(kind == CodeForConstruct); - return hasJITCodeForConstruct(); + return result; + } + + static ptrdiff_t offsetOfJITCodeWithArityCheckFor( + CodeSpecializationKind kind, RegisterPreservationMode registers) + { + switch (kind) { + case CodeForCall: + switch (registers) { + case RegisterPreservationNotRequired: + return OBJECT_OFFSETOF(ExecutableBase, m_jitCodeForCallWithArityCheck); + case MustPreserveRegisters: + return OBJECT_OFFSETOF(ExecutableBase, m_jitCodeForCallWithArityCheckAndPreserveRegs); + } + case CodeForConstruct: + switch (registers) { + case RegisterPreservationNotRequired: + return OBJECT_OFFSETOF(ExecutableBase, m_jitCodeForConstructWithArityCheck); + case MustPreserveRegisters: + return OBJECT_OFFSETOF(ExecutableBase, m_jitCodeForConstructWithArityCheckAndPreserveRegs); + } } + RELEASE_ASSERT_NOT_REACHED(); + return 0; + } + + static ptrdiff_t offsetOfNumParametersFor(CodeSpecializationKind kind) + { + if (kind == CodeForCall) + return OBJECT_OFFSETOF(ExecutableBase, m_numParametersForCall); + ASSERT(kind == CodeForConstruct); + return OBJECT_OFFSETOF(ExecutableBase, m_numParametersForConstruct); + } - // Intrinsics are only for calls, currently. - Intrinsic intrinsic() const; + bool hasJITCodeForCall() const + { + return m_numParametersForCall >= 0; + } - Intrinsic intrinsicFor(CodeSpecializationKind kind) const - { - if (isCall(kind)) - return intrinsic(); - return NoIntrinsic; - } + bool hasJITCodeForConstruct() const + { + return m_numParametersForConstruct >= 0; + } + + bool hasJITCodeFor(CodeSpecializationKind kind) const + { + if (kind == CodeForCall) + return hasJITCodeForCall(); + ASSERT(kind == CodeForConstruct); + return hasJITCodeForConstruct(); + } - protected: - JITCode m_jitCodeForCall; - JITCode m_jitCodeForConstruct; - MacroAssemblerCodePtr m_jitCodeForCallWithArityCheck; - MacroAssemblerCodePtr m_jitCodeForConstructWithArityCheck; -#endif - void clearCode(); - }; - - class NativeExecutable : public ExecutableBase { - friend class JIT; - friend class LLIntOffsetsExtractor; - public: - typedef ExecutableBase Base; - -#if ENABLE(JIT) - static NativeExecutable* create(JSGlobalData& globalData, MacroAssemblerCodeRef callThunk, NativeFunction function, MacroAssemblerCodeRef constructThunk, NativeFunction constructor, Intrinsic intrinsic) - { - ASSERT(!globalData.interpreter->classicEnabled()); - NativeExecutable* executable; - if (!callThunk) { - executable = new (NotNull, allocateCell(globalData.heap)) NativeExecutable(globalData, function, constructor); - executable->finishCreation(globalData, JITCode(), JITCode(), intrinsic); - } else { - executable = new (NotNull, allocateCell(globalData.heap)) NativeExecutable(globalData, function, constructor); - executable->finishCreation(globalData, JITCode::HostFunction(callThunk), JITCode::HostFunction(constructThunk), intrinsic); - } - globalData.heap.addFinalizer(executable, &finalize); - return executable; - } -#endif + // Intrinsics are only for calls, currently. + Intrinsic intrinsic() const; + + Intrinsic intrinsicFor(CodeSpecializationKind kind) const + { + if (isCall(kind)) + return intrinsic(); + return NoIntrinsic; + } + + void dump(PrintStream&) const; + +protected: + RefPtr m_jitCodeForCall; + RefPtr m_jitCodeForConstruct; + MacroAssemblerCodePtr m_jitCodeForCallWithArityCheck; + MacroAssemblerCodePtr m_jitCodeForConstructWithArityCheck; + MacroAssemblerCodePtr m_jitCodeForCallWithArityCheckAndPreserveRegs; + MacroAssemblerCodePtr m_jitCodeForConstructWithArityCheckAndPreserveRegs; +}; + +class NativeExecutable final : public ExecutableBase { + friend class JIT; + friend class LLIntOffsetsExtractor; +public: + typedef ExecutableBase Base; + static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal; + + static NativeExecutable* create(VM& vm, PassRefPtr callThunk, NativeFunction function, PassRefPtr constructThunk, NativeFunction constructor, Intrinsic intrinsic) + { + NativeExecutable* executable; + executable = new (NotNull, allocateCell(vm.heap)) NativeExecutable(vm, function, constructor); + executable->finishCreation(vm, callThunk, constructThunk, intrinsic); + return executable; + } -#if ENABLE(CLASSIC_INTERPRETER) - static NativeExecutable* create(JSGlobalData& globalData, NativeFunction function, NativeFunction constructor) - { - ASSERT(!globalData.canUseJIT()); - NativeExecutable* executable = new (NotNull, allocateCell(globalData.heap)) NativeExecutable(globalData, function, constructor); - executable->finishCreation(globalData); - globalData.heap.addFinalizer(executable, &finalize); - return executable; - } -#endif + static void destroy(JSCell*); -#if ENABLE(JIT) - static void destroy(JSCell*); -#endif + CodeBlockHash hashFor(CodeSpecializationKind) const; - NativeFunction function() { return m_function; } - NativeFunction constructor() { return m_constructor; } + NativeFunction function() { return m_function; } + NativeFunction constructor() { return m_constructor; } + + NativeFunction nativeFunctionFor(CodeSpecializationKind kind) + { + if (kind == CodeForCall) + return function(); + ASSERT(kind == CodeForConstruct); + return constructor(); + } + + static ptrdiff_t offsetOfNativeFunctionFor(CodeSpecializationKind kind) + { + if (kind == CodeForCall) + return OBJECT_OFFSETOF(NativeExecutable, m_function); + ASSERT(kind == CodeForConstruct); + return OBJECT_OFFSETOF(NativeExecutable, m_constructor); + } - static Structure* createStructure(JSGlobalData& globalData, JSGlobalObject* globalObject, JSValue proto) { return Structure::create(globalData, globalObject, proto, TypeInfo(LeafType, StructureFlags), &s_info); } + static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) { return Structure::create(vm, globalObject, proto, TypeInfo(CellType, StructureFlags), info()); } - static const ClassInfo s_info; - - Intrinsic intrinsic() const; - - protected: -#if ENABLE(JIT) - void finishCreation(JSGlobalData& globalData, JITCode callThunk, JITCode constructThunk, Intrinsic intrinsic) - { - ASSERT(!globalData.interpreter->classicEnabled()); - Base::finishCreation(globalData); - m_jitCodeForCall = callThunk; - m_jitCodeForConstruct = constructThunk; - m_jitCodeForCallWithArityCheck = callThunk.addressForCall(); - m_jitCodeForConstructWithArityCheck = constructThunk.addressForCall(); - m_intrinsic = intrinsic; - } -#endif + DECLARE_INFO; -#if ENABLE(CLASSIC_INTERPRETER) - void finishCreation(JSGlobalData& globalData) - { - ASSERT(!globalData.canUseJIT()); - Base::finishCreation(globalData); - m_intrinsic = NoIntrinsic; - } -#endif + Intrinsic intrinsic() const; - static void finalize(JSCell*); - - private: - NativeExecutable(JSGlobalData& globalData, NativeFunction function, NativeFunction constructor) - : ExecutableBase(globalData, globalData.nativeExecutableStructure.get(), NUM_PARAMETERS_IS_HOST) - , m_function(function) - , m_constructor(constructor) - { - } +protected: + void finishCreation(VM& vm, PassRefPtr callThunk, PassRefPtr constructThunk, Intrinsic intrinsic) + { + Base::finishCreation(vm); + m_jitCodeForCall = callThunk; + m_jitCodeForConstruct = constructThunk; + m_intrinsic = intrinsic; + } + +private: + NativeExecutable(VM& vm, NativeFunction function, NativeFunction constructor) + : ExecutableBase(vm, vm.nativeExecutableStructure.get(), NUM_PARAMETERS_IS_HOST) + , m_function(function) + , m_constructor(constructor) + { + } + + NativeFunction m_function; + NativeFunction m_constructor; + + Intrinsic m_intrinsic; +}; + +class ScriptExecutable : public ExecutableBase { +public: + typedef ExecutableBase Base; + static const unsigned StructureFlags = Base::StructureFlags; + + static void destroy(JSCell*); + + CodeBlockHash hashFor(CodeSpecializationKind) const; + + const SourceCode& source() const { return m_source; } + intptr_t sourceID() const { return m_source.providerID(); } + const String& sourceURL() const { return m_source.provider()->url(); } + int firstLine() const { return m_firstLine; } + void setOverrideLineNumber(int overrideLineNumber) { m_overrideLineNumber = overrideLineNumber; } + bool hasOverrideLineNumber() const { return m_overrideLineNumber != -1; } + int overrideLineNumber() const { return m_overrideLineNumber; } + int lastLine() const { return m_lastLine; } + unsigned startColumn() const { return m_startColumn; } + unsigned endColumn() const { return m_endColumn; } + unsigned typeProfilingStartOffset() const { return m_typeProfilingStartOffset; } + unsigned typeProfilingEndOffset() const { return m_typeProfilingEndOffset; } + + bool usesEval() const { return m_features & EvalFeature; } + bool usesArguments() const { return m_features & ArgumentsFeature; } + bool needsActivation() const { return m_hasCapturedVariables || m_features & (EvalFeature | WithFeature | CatchFeature); } + bool isStrictMode() const { return m_features & StrictModeFeature; } + ECMAMode ecmaMode() const { return isStrictMode() ? StrictMode : NotStrictMode; } + + void setNeverInline(bool value) { m_neverInline = value; } + void setDidTryToEnterInLoop(bool value) { m_didTryToEnterInLoop = value; } + bool neverInline() const { return m_neverInline; } + bool didTryToEnterInLoop() const { return m_didTryToEnterInLoop; } + bool isInliningCandidate() const { return !neverInline(); } + + bool* addressOfDidTryToEnterInLoop() { return &m_didTryToEnterInLoop; } - NativeFunction m_function; - NativeFunction m_constructor; + void unlinkCalls(); - Intrinsic m_intrinsic; - }; - - class ScriptExecutable : public ExecutableBase { - public: - typedef ExecutableBase Base; - - ScriptExecutable(Structure* structure, JSGlobalData& globalData, const SourceCode& source, bool isInStrictContext) - : ExecutableBase(globalData, structure, NUM_PARAMETERS_NOT_COMPILED) - , m_source(source) - , m_features(isInStrictContext ? StrictModeFeature : 0) - { - } + CodeFeatures features() const { return m_features; } + + DECLARE_INFO; - ScriptExecutable(Structure* structure, ExecState* exec, const SourceCode& source, bool isInStrictContext) - : ExecutableBase(exec->globalData(), structure, NUM_PARAMETERS_NOT_COMPILED) - , m_source(source) - , m_features(isInStrictContext ? StrictModeFeature : 0) - { - } + void recordParse(CodeFeatures features, bool hasCapturedVariables, int firstLine, int lastLine, unsigned startColumn, unsigned endColumn) + { + m_features = features; + m_hasCapturedVariables = hasCapturedVariables; + m_firstLine = firstLine; + m_lastLine = lastLine; + ASSERT(startColumn != UINT_MAX); + m_startColumn = startColumn; + ASSERT(endColumn != UINT_MAX); + m_endColumn = endColumn; + } -#if ENABLE(JIT) - static void destroy(JSCell*); -#endif + void installCode(CodeBlock*); + RefPtr newCodeBlockFor(CodeSpecializationKind, JSFunction*, JSScope*, JSObject*& exception); + PassRefPtr newReplacementCodeBlockFor(CodeSpecializationKind); + + JSObject* prepareForExecution(ExecState* exec, JSFunction* function, JSScope* scope, CodeSpecializationKind kind) + { + if (hasJITCodeFor(kind)) + return 0; + return prepareForExecutionImpl(exec, function, scope, kind); + } - const SourceCode& source() { return m_source; } - intptr_t sourceID() const { return m_source.providerID(); } - const UString& sourceURL() const { return m_source.provider()->url(); } - int lineNo() const { return m_firstLine; } - int lastLine() const { return m_lastLine; } + template void forEachCodeBlock(Functor&&); - bool usesEval() const { return m_features & EvalFeature; } - bool usesArguments() const { return m_features & ArgumentsFeature; } - bool needsActivation() const { return m_hasCapturedVariables || m_features & (EvalFeature | WithFeature | CatchFeature); } - bool isStrictMode() const { return m_features & StrictModeFeature; } +private: + JSObject* prepareForExecutionImpl(ExecState*, JSFunction*, JSScope*, CodeSpecializationKind); - void unlinkCalls(); - - static const ClassInfo s_info; +protected: + ScriptExecutable(Structure* structure, VM& vm, const SourceCode& source, bool isInStrictContext); + + void finishCreation(VM& vm) + { + Base::finishCreation(vm); + vm.heap.addCompiledCode(this); // Balanced by Heap::deleteUnmarkedCompiledCode(). - protected: - void finishCreation(JSGlobalData& globalData) - { - Base::finishCreation(globalData); #if ENABLE(CODEBLOCK_SAMPLING) - if (SamplingTool* sampler = globalData.interpreter->sampler()) - sampler->notifyOfScope(globalData, this); + if (SamplingTool* sampler = vm.interpreter->sampler()) + sampler->notifyOfScope(vm, this); #endif - } - - void recordParse(CodeFeatures features, bool hasCapturedVariables, int firstLine, int lastLine) - { - m_features = features; - m_hasCapturedVariables = hasCapturedVariables; - m_firstLine = firstLine; - m_lastLine = lastLine; - } + } - SourceCode m_source; - CodeFeatures m_features; - bool m_hasCapturedVariables; - int m_firstLine; - int m_lastLine; - }; - - class EvalExecutable : public ScriptExecutable { - friend class LLIntOffsetsExtractor; - public: - typedef ScriptExecutable Base; - - static void destroy(JSCell*); - - JSObject* compile(ExecState* exec, ScopeChainNode* scopeChainNode) - { - ASSERT(exec->globalData().dynamicGlobalObject); - JSObject* error = 0; - if (!m_evalCodeBlock) - error = compileInternal(exec, scopeChainNode, JITCode::bottomTierJIT()); - ASSERT(!error == !!m_evalCodeBlock); - return error; - } - - JSObject* compileOptimized(ExecState*, ScopeChainNode*); - -#if ENABLE(JIT) - void jettisonOptimizedCode(JSGlobalData&); - bool jitCompile(JSGlobalData&); -#endif + SourceCode m_source; + CodeFeatures m_features; + bool m_hasCapturedVariables; + bool m_neverInline; + bool m_didTryToEnterInLoop; + int m_overrideLineNumber; + int m_firstLine; + int m_lastLine; + unsigned m_startColumn; + unsigned m_endColumn; + unsigned m_typeProfilingStartOffset; + unsigned m_typeProfilingEndOffset; +}; + +class EvalExecutable final : public ScriptExecutable { + friend class LLIntOffsetsExtractor; +public: + typedef ScriptExecutable Base; + static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal; + + static void destroy(JSCell*); + + EvalCodeBlock* codeBlock() + { + return m_evalCodeBlock.get(); + } - EvalCodeBlock& generatedBytecode() - { - ASSERT(m_evalCodeBlock); - return *m_evalCodeBlock; - } + static EvalExecutable* create(ExecState*, const SourceCode&, bool isInStrictContext, ThisTDZMode); - static EvalExecutable* create(ExecState* exec, const SourceCode& source, bool isInStrictContext) - { - EvalExecutable* executable = new (NotNull, allocateCell(*exec->heap())) EvalExecutable(exec, source, isInStrictContext); - executable->finishCreation(exec->globalData()); - exec->globalData().heap.addFinalizer(executable, &finalize); - return executable; - } + PassRefPtr generatedJITCode() + { + return generatedJITCodeForCall(); + } -#if ENABLE(JIT) - JITCode& generatedJITCode() - { - return generatedJITCodeForCall(); - } -#endif - static Structure* createStructure(JSGlobalData& globalData, JSGlobalObject* globalObject, JSValue proto) - { - return Structure::create(globalData, globalObject, proto, TypeInfo(EvalExecutableType, StructureFlags), &s_info); - } + static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) + { + return Structure::create(vm, globalObject, proto, TypeInfo(EvalExecutableType, StructureFlags), info()); + } - static const ClassInfo s_info; + DECLARE_INFO; - void unlinkCalls(); + void unlinkCalls(); - protected: - void clearCode(); - static void finalize(JSCell*); + void clearCode(); - private: - static const unsigned StructureFlags = OverridesVisitChildren | ScriptExecutable::StructureFlags; - EvalExecutable(ExecState*, const SourceCode&, bool); + ExecutableInfo executableInfo() const { return ExecutableInfo(needsActivation(), usesEval(), isStrictMode(), false, false, ConstructorKind::None); } - JSObject* compileInternal(ExecState*, ScopeChainNode*, JITCode::JITType); - static void visitChildren(JSCell*, SlotVisitor&); + unsigned numVariables() { return m_unlinkedEvalCodeBlock->numVariables(); } + unsigned numberOfFunctionDecls() { return m_unlinkedEvalCodeBlock->numberOfFunctionDecls(); } - OwnPtr m_evalCodeBlock; - }; +private: + friend class ScriptExecutable; + EvalExecutable(ExecState*, const SourceCode&, bool); - class ProgramExecutable : public ScriptExecutable { - friend class LLIntOffsetsExtractor; - public: - typedef ScriptExecutable Base; + static void visitChildren(JSCell*, SlotVisitor&); - static ProgramExecutable* create(ExecState* exec, const SourceCode& source) - { - ProgramExecutable* executable = new (NotNull, allocateCell(*exec->heap())) ProgramExecutable(exec, source); - executable->finishCreation(exec->globalData()); - exec->globalData().heap.addFinalizer(executable, &finalize); - return executable; - } + RefPtr m_evalCodeBlock; + WriteBarrier m_unlinkedEvalCodeBlock; +}; - static void destroy(JSCell*); +class ProgramExecutable final : public ScriptExecutable { + friend class LLIntOffsetsExtractor; +public: + typedef ScriptExecutable Base; + static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal; - JSObject* compile(ExecState* exec, ScopeChainNode* scopeChainNode) - { - ASSERT(exec->globalData().dynamicGlobalObject); - JSObject* error = 0; - if (!m_programCodeBlock) - error = compileInternal(exec, scopeChainNode, JITCode::bottomTierJIT()); - ASSERT(!error == !!m_programCodeBlock); - return error; - } - - JSObject* compileOptimized(ExecState*, ScopeChainNode*); - -#if ENABLE(JIT) - void jettisonOptimizedCode(JSGlobalData&); - bool jitCompile(JSGlobalData&); -#endif + static ProgramExecutable* create(ExecState* exec, const SourceCode& source) + { + ProgramExecutable* executable = new (NotNull, allocateCell(*exec->heap())) ProgramExecutable(exec, source); + executable->finishCreation(exec->vm()); + return executable; + } - ProgramCodeBlock& generatedBytecode() - { - ASSERT(m_programCodeBlock); - return *m_programCodeBlock; - } - JSObject* checkSyntax(ExecState*); + JSObject* initializeGlobalProperties(VM&, CallFrame*, JSScope*); -#if ENABLE(JIT) - JITCode& generatedJITCode() - { - return generatedJITCodeForCall(); - } -#endif - - static Structure* createStructure(JSGlobalData& globalData, JSGlobalObject* globalObject, JSValue proto) - { - return Structure::create(globalData, globalObject, proto, TypeInfo(ProgramExecutableType, StructureFlags), &s_info); - } - - static const ClassInfo s_info; - - void unlinkCalls(); - - protected: - void clearCode(); - static void finalize(JSCell*); - - private: - static const unsigned StructureFlags = OverridesVisitChildren | ScriptExecutable::StructureFlags; - ProgramExecutable(ExecState*, const SourceCode&); - - JSObject* compileInternal(ExecState*, ScopeChainNode*, JITCode::JITType); - static void visitChildren(JSCell*, SlotVisitor&); - - OwnPtr m_programCodeBlock; - }; - - class FunctionExecutable : public ScriptExecutable, public DoublyLinkedListNode { - friend class JIT; - friend class LLIntOffsetsExtractor; - friend class WTF::DoublyLinkedListNode; - public: - typedef ScriptExecutable Base; - - static FunctionExecutable* create(ExecState* exec, const Identifier& name, const Identifier& inferredName, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool isInStrictContext, int firstLine, int lastLine) - { - FunctionExecutable* executable = new (NotNull, allocateCell(*exec->heap())) FunctionExecutable(exec, name, inferredName, source, forceUsesArguments, parameters, isInStrictContext); - executable->finishCreation(exec->globalData(), name, firstLine, lastLine); - exec->globalData().heap.addFunctionExecutable(executable); - exec->globalData().heap.addFinalizer(executable, &finalize); - return executable; - } + static void destroy(JSCell*); - static FunctionExecutable* create(JSGlobalData& globalData, const Identifier& name, const Identifier& inferredName, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool isInStrictContext, int firstLine, int lastLine) - { - FunctionExecutable* executable = new (NotNull, allocateCell(globalData.heap)) FunctionExecutable(globalData, name, inferredName, source, forceUsesArguments, parameters, isInStrictContext); - executable->finishCreation(globalData, name, firstLine, lastLine); - globalData.heap.addFunctionExecutable(executable); - globalData.heap.addFinalizer(executable, &finalize); - return executable; - } + ProgramCodeBlock* codeBlock() + { + return m_programCodeBlock.get(); + } - static void destroy(JSCell*); + JSObject* checkSyntax(ExecState*); - JSFunction* make(ExecState* exec, ScopeChainNode* scopeChain) - { - return JSFunction::create(exec, this, scopeChain); - } + PassRefPtr generatedJITCode() + { + return generatedJITCodeForCall(); + } - // Returns either call or construct bytecode. This can be appropriate - // for answering questions that that don't vary between call and construct -- - // for example, argumentsRegister(). - FunctionCodeBlock& generatedBytecode() - { - if (m_codeBlockForCall) - return *m_codeBlockForCall; - ASSERT(m_codeBlockForConstruct); - return *m_codeBlockForConstruct; - } + static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) + { + return Structure::create(vm, globalObject, proto, TypeInfo(ProgramExecutableType, StructureFlags), info()); + } - FunctionCodeBlock* codeBlockWithBytecodeFor(CodeSpecializationKind); + DECLARE_INFO; - PassOwnPtr produceCodeBlockFor(ScopeChainNode*, CompilationKind, CodeSpecializationKind, JSObject*& exception); - - JSObject* compileForCall(ExecState* exec, ScopeChainNode* scopeChainNode) - { - ASSERT(exec->globalData().dynamicGlobalObject); - JSObject* error = 0; - if (!m_codeBlockForCall) - error = compileForCallInternal(exec, scopeChainNode, JITCode::bottomTierJIT()); - ASSERT(!error == !!m_codeBlockForCall); - return error; - } + void unlinkCalls(); - JSObject* compileOptimizedForCall(ExecState*, ScopeChainNode*); - -#if ENABLE(JIT) - void jettisonOptimizedCodeForCall(JSGlobalData&); - bool jitCompileForCall(JSGlobalData&); -#endif + void clearCode(); - bool isGeneratedForCall() const - { - return m_codeBlockForCall; - } + ExecutableInfo executableInfo() const { return ExecutableInfo(needsActivation(), usesEval(), isStrictMode(), false, false, ConstructorKind::None); } - FunctionCodeBlock& generatedBytecodeForCall() - { - ASSERT(m_codeBlockForCall); - return *m_codeBlockForCall; - } +private: + friend class ScriptExecutable; - JSObject* compileForConstruct(ExecState* exec, ScopeChainNode* scopeChainNode) - { - ASSERT(exec->globalData().dynamicGlobalObject); - JSObject* error = 0; - if (!m_codeBlockForConstruct) - error = compileForConstructInternal(exec, scopeChainNode, JITCode::bottomTierJIT()); - ASSERT(!error == !!m_codeBlockForConstruct); - return error; - } + ProgramExecutable(ExecState*, const SourceCode&); - JSObject* compileOptimizedForConstruct(ExecState*, ScopeChainNode*); - -#if ENABLE(JIT) - void jettisonOptimizedCodeForConstruct(JSGlobalData&); - bool jitCompileForConstruct(JSGlobalData&); -#endif + static void visitChildren(JSCell*, SlotVisitor&); - bool isGeneratedForConstruct() const - { - return m_codeBlockForConstruct; - } + WriteBarrier m_unlinkedProgramCodeBlock; + RefPtr m_programCodeBlock; +}; - FunctionCodeBlock& generatedBytecodeForConstruct() - { - ASSERT(m_codeBlockForConstruct); - return *m_codeBlockForConstruct; - } - - JSObject* compileFor(ExecState* exec, ScopeChainNode* scopeChainNode, CodeSpecializationKind kind) - { - ASSERT(exec->callee()); - ASSERT(exec->callee()->inherits(&JSFunction::s_info)); - ASSERT(jsCast(exec->callee())->jsExecutable() == this); - - if (kind == CodeForCall) - return compileForCall(exec, scopeChainNode); - ASSERT(kind == CodeForConstruct); - return compileForConstruct(exec, scopeChainNode); - } - - JSObject* compileOptimizedFor(ExecState* exec, ScopeChainNode* scopeChainNode, CodeSpecializationKind kind) - { - ASSERT(exec->callee()); - ASSERT(exec->callee()->inherits(&JSFunction::s_info)); - ASSERT(jsCast(exec->callee())->jsExecutable() == this); - - if (kind == CodeForCall) - return compileOptimizedForCall(exec, scopeChainNode); - ASSERT(kind == CodeForConstruct); - return compileOptimizedForConstruct(exec, scopeChainNode); - } +class FunctionExecutable final : public ScriptExecutable { + friend class JIT; + friend class LLIntOffsetsExtractor; +public: + typedef ScriptExecutable Base; + static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal; - -#if ENABLE(JIT) - void jettisonOptimizedCodeFor(JSGlobalData& globalData, CodeSpecializationKind kind) - { - if (kind == CodeForCall) - jettisonOptimizedCodeForCall(globalData); - else { - ASSERT(kind == CodeForConstruct); - jettisonOptimizedCodeForConstruct(globalData); - } - } - - bool jitCompileFor(JSGlobalData& globalData, CodeSpecializationKind kind) - { - if (kind == CodeForCall) - return jitCompileForCall(globalData); - ASSERT(kind == CodeForConstruct); - return jitCompileForConstruct(globalData); - } -#endif - - bool isGeneratedFor(CodeSpecializationKind kind) - { - if (kind == CodeForCall) - return isGeneratedForCall(); - ASSERT(kind == CodeForConstruct); - return isGeneratedForConstruct(); - } - - FunctionCodeBlock& generatedBytecodeFor(CodeSpecializationKind kind) - { - if (kind == CodeForCall) - return generatedBytecodeForCall(); - ASSERT(kind == CodeForConstruct); - return generatedBytecodeForConstruct(); - } + static FunctionExecutable* create( + VM& vm, const SourceCode& source, UnlinkedFunctionExecutable* unlinkedExecutable, + unsigned firstLine, unsigned lastLine, unsigned startColumn, unsigned endColumn) + { + FunctionExecutable* executable = new (NotNull, allocateCell(vm.heap)) FunctionExecutable(vm, source, unlinkedExecutable, firstLine, lastLine, startColumn, endColumn); + executable->finishCreation(vm); + return executable; + } + static FunctionExecutable* fromGlobalCode( + const Identifier& name, ExecState&, const SourceCode&, + JSObject*& exception, int overrideLineNumber); - FunctionCodeBlock* baselineCodeBlockFor(CodeSpecializationKind); - - FunctionCodeBlock* profiledCodeBlockFor(CodeSpecializationKind kind) - { - return baselineCodeBlockFor(kind); - } - - const Identifier& name() { return m_name; } - const Identifier& inferredName() { return m_inferredName; } - JSString* nameValue() const { return m_nameValue.get(); } - size_t parameterCount() const { return m_parameters->size(); } // Excluding 'this'! - unsigned capturedVariableCount() const { return m_numCapturedVariables; } - UString paramString() const; - SharedSymbolTable* symbolTable() const { return m_symbolTable; } - - void discardCode(); - static void visitChildren(JSCell*, SlotVisitor&); - static FunctionExecutable* fromGlobalCode(const Identifier&, ExecState*, Debugger*, const SourceCode&, JSObject** exception); - static Structure* createStructure(JSGlobalData& globalData, JSGlobalObject* globalObject, JSValue proto) - { - return Structure::create(globalData, globalObject, proto, TypeInfo(FunctionExecutableType, StructureFlags), &s_info); - } + static void destroy(JSCell*); - static const ClassInfo s_info; - - void unlinkCalls(); - - protected: - void clearCode(); - static void finalize(JSCell*); - - void finishCreation(JSGlobalData& globalData, const Identifier& name, int firstLine, int lastLine) - { - Base::finishCreation(globalData); - m_firstLine = firstLine; - m_lastLine = lastLine; - m_nameValue.set(globalData, this, jsString(&globalData, name.ustring())); - } - - private: - FunctionExecutable(JSGlobalData&, const Identifier& name, const Identifier& inferredName, const SourceCode&, bool forceUsesArguments, FunctionParameters*, bool); - FunctionExecutable(ExecState*, const Identifier& name, const Identifier& inferredName, const SourceCode&, bool forceUsesArguments, FunctionParameters*, bool); + UnlinkedFunctionExecutable* unlinkedExecutable() + { + return m_unlinkedExecutable.get(); + } - JSObject* compileForCallInternal(ExecState*, ScopeChainNode*, JITCode::JITType); - JSObject* compileForConstructInternal(ExecState*, ScopeChainNode*, JITCode::JITType); - - OwnPtr& codeBlockFor(CodeSpecializationKind kind) - { - if (kind == CodeForCall) - return m_codeBlockForCall; - ASSERT(kind == CodeForConstruct); - return m_codeBlockForConstruct; - } + // Returns either call or construct bytecode. This can be appropriate + // for answering questions that that don't vary between call and construct -- + // for example, argumentsRegister(). + FunctionCodeBlock* eitherCodeBlock() + { + if (m_codeBlockForCall) + return m_codeBlockForCall.get(); + return m_codeBlockForConstruct.get(); + } - static const unsigned StructureFlags = OverridesVisitChildren | ScriptExecutable::StructureFlags; - unsigned m_numCapturedVariables : 31; - bool m_forceUsesArguments : 1; - - RefPtr m_parameters; - OwnPtr m_codeBlockForCall; - OwnPtr m_codeBlockForConstruct; - Identifier m_name; - Identifier m_inferredName; - WriteBarrier m_nameValue; - SharedSymbolTable* m_symbolTable; - FunctionExecutable* m_next; - FunctionExecutable* m_prev; - }; + bool isGeneratedForCall() const + { + return m_codeBlockForCall; + } - inline FunctionExecutable* JSFunction::jsExecutable() const + FunctionCodeBlock* codeBlockForCall() { - ASSERT(!isHostFunctionNonInline()); - return static_cast(m_executable.get()); + return m_codeBlockForCall.get(); } - inline bool JSFunction::isHostFunction() const + bool isGeneratedForConstruct() const { - ASSERT(m_executable); - return m_executable->isHostFunction(); + return m_codeBlockForConstruct; } - inline NativeFunction JSFunction::nativeFunction() + FunctionCodeBlock* codeBlockForConstruct() { - ASSERT(isHostFunction()); - return static_cast(m_executable.get())->function(); + return m_codeBlockForConstruct.get(); + } + + bool isGeneratedFor(CodeSpecializationKind kind) + { + if (kind == CodeForCall) + return isGeneratedForCall(); + ASSERT(kind == CodeForConstruct); + return isGeneratedForConstruct(); + } + + FunctionCodeBlock* codeBlockFor(CodeSpecializationKind kind) + { + if (kind == CodeForCall) + return codeBlockForCall(); + ASSERT(kind == CodeForConstruct); + return codeBlockForConstruct(); } - inline NativeFunction JSFunction::nativeConstructor() + FunctionCodeBlock* baselineCodeBlockFor(CodeSpecializationKind); + + FunctionCodeBlock* profiledCodeBlockFor(CodeSpecializationKind kind) { - ASSERT(isHostFunction()); - return static_cast(m_executable.get())->constructor(); + return baselineCodeBlockFor(kind); } - inline bool isHostFunction(JSValue value, NativeFunction nativeFunction) + RefPtr returnStatementTypeSet() { - JSFunction* function = jsCast(getJSFunction(value)); - if (!function || !function->isHostFunction()) - return false; - return function->nativeFunction() == nativeFunction; + if (!m_returnStatementTypeSet) + m_returnStatementTypeSet = TypeSet::create(); + + return m_returnStatementTypeSet; } + + FunctionMode functionMode() { return m_unlinkedExecutable->functionMode(); } + bool isBuiltinFunction() const { return m_unlinkedExecutable->isBuiltinFunction(); } + bool isClassConstructorFunction() const { return m_unlinkedExecutable->isClassConstructorFunction(); } + const Identifier& name() { return m_unlinkedExecutable->name(); } + const Identifier& inferredName() { return m_unlinkedExecutable->inferredName(); } + JSString* nameValue() const { return m_unlinkedExecutable->nameValue(); } + size_t parameterCount() const { return m_unlinkedExecutable->parameterCount(); } // Excluding 'this'! + SymbolTable* symbolTable(CodeSpecializationKind); + + void clearUnlinkedCodeForRecompilation(); + static void visitChildren(JSCell*, SlotVisitor&); + static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue proto) + { + return Structure::create(vm, globalObject, proto, TypeInfo(FunctionExecutableType, StructureFlags), info()); + } + + unsigned parametersStartOffset() const { return m_parametersStartOffset; } - inline void ScriptExecutable::unlinkCalls() + void overrideParameterAndTypeProfilingStartEndOffsets(unsigned parametersStartOffset, unsigned typeProfilingStartOffset, unsigned typeProfilingEndOffset) { - switch (structure()->typeInfo().type()) { - case EvalExecutableType: - return jsCast(this)->unlinkCalls(); - case ProgramExecutableType: - return jsCast(this)->unlinkCalls(); - case FunctionExecutableType: - return jsCast(this)->unlinkCalls(); - default: - ASSERT_NOT_REACHED(); - } + m_parametersStartOffset = parametersStartOffset; + m_typeProfilingStartOffset = typeProfilingStartOffset; + m_typeProfilingEndOffset = typeProfilingEndOffset; + } + + DECLARE_INFO; + + void unlinkCalls(); + + void clearCode(); + + InferredValue* singletonFunction() { return m_singletonFunction.get(); } + +private: + FunctionExecutable( + VM&, const SourceCode&, UnlinkedFunctionExecutable*, unsigned firstLine, + unsigned lastLine, unsigned startColumn, unsigned endColumn); + + void finishCreation(VM&); + + friend class ScriptExecutable; + + WriteBarrier m_unlinkedExecutable; + RefPtr m_codeBlockForCall; + RefPtr m_codeBlockForConstruct; + RefPtr m_returnStatementTypeSet; + unsigned m_parametersStartOffset; + WriteBarrier m_singletonFunction; +}; + +inline void ExecutableBase::clearCodeVirtual(ExecutableBase* executable) +{ + switch (executable->type()) { + case EvalExecutableType: + return jsCast(executable)->clearCode(); + case ProgramExecutableType: + return jsCast(executable)->clearCode(); + case FunctionExecutableType: + return jsCast(executable)->clearCode(); + default: + return jsCast(executable)->clearCode(); } +} + +inline void ScriptExecutable::unlinkCalls() +{ + switch (type()) { + case EvalExecutableType: + return jsCast(this)->unlinkCalls(); + case ProgramExecutableType: + return jsCast(this)->unlinkCalls(); + case FunctionExecutableType: + return jsCast(this)->unlinkCalls(); + default: + RELEASE_ASSERT_NOT_REACHED(); + } +} }