/* * Copyright (C) 2008-2009, 2012-2016 Apple Inc. All rights reserved. * Copyright (C) 2008 Cameron Zwarich * Copyright (C) 2012 Igalia, S.L. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of Apple Inc. ("Apple") nor the names of * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef BytecodeGenerator_h #define BytecodeGenerator_h #include "CodeBlock.h" #include #include "Instruction.h" #include "Label.h" #include "LabelScope.h" #include "Interpreter.h" #include "ParserError.h" #include "RegisterID.h" #include "SetForScope.h" #include "SymbolTable.h" #include "Debugger.h" #include "Nodes.h" #include "StaticPropertyAnalyzer.h" #include "TemplateRegistryKey.h" #include "UnlinkedCodeBlock.h" #include #include #include #include namespace JSC { class Identifier; class JSTemplateRegistryKey; enum ExpectedFunction { NoExpectedFunction, ExpectObjectConstructor, ExpectArrayConstructor }; enum class ThisResolutionType { Local, Scoped }; class CallArguments { public: CallArguments(BytecodeGenerator&, ArgumentsNode*, unsigned additionalArguments = 0); RegisterID* thisRegister() { return m_argv[0].get(); } RegisterID* argumentRegister(unsigned i) { return m_argv[i + 1].get(); } unsigned stackOffset() { return -m_argv[0]->index() + JSStack::CallFrameHeaderSize; } unsigned argumentCountIncludingThis() { return m_argv.size() - m_padding; } ArgumentsNode* argumentsNode() { return m_argumentsNode; } private: ArgumentsNode* m_argumentsNode; Vector, 8, UnsafeVectorOverflow> m_argv; unsigned m_padding; }; struct FinallyContext { StatementNode* finallyBlock; RegisterID* iterator; ThrowableExpressionData* enumerationNode; unsigned scopeContextStackSize; unsigned switchContextStackSize; unsigned forInContextStackSize; unsigned tryContextStackSize; unsigned labelScopesSize; unsigned symbolTableStackSize; int finallyDepth; int dynamicScopeDepth; }; struct ControlFlowContext { bool isFinallyBlock; FinallyContext finallyContext; }; class ForInContext { WTF_MAKE_FAST_ALLOCATED; public: ForInContext(RegisterID* localRegister) : m_localRegister(localRegister) , m_isValid(true) { } virtual ~ForInContext() { } bool isValid() const { return m_isValid; } void invalidate() { m_isValid = false; } enum ForInContextType { StructureForInContextType, IndexedForInContextType }; virtual ForInContextType type() const = 0; RegisterID* local() const { return m_localRegister.get(); } private: RefPtr m_localRegister; bool m_isValid; }; class StructureForInContext : public ForInContext { public: StructureForInContext(RegisterID* localRegister, RegisterID* indexRegister, RegisterID* propertyRegister, RegisterID* enumeratorRegister) : ForInContext(localRegister) , m_indexRegister(indexRegister) , m_propertyRegister(propertyRegister) , m_enumeratorRegister(enumeratorRegister) { } virtual ForInContextType type() const { return StructureForInContextType; } RegisterID* index() const { return m_indexRegister.get(); } RegisterID* property() const { return m_propertyRegister.get(); } RegisterID* enumerator() const { return m_enumeratorRegister.get(); } private: RefPtr m_indexRegister; RefPtr m_propertyRegister; RefPtr m_enumeratorRegister; }; class IndexedForInContext : public ForInContext { public: IndexedForInContext(RegisterID* localRegister, RegisterID* indexRegister) : ForInContext(localRegister) , m_indexRegister(indexRegister) { } virtual ForInContextType type() const { return IndexedForInContextType; } RegisterID* index() const { return m_indexRegister.get(); } private: RefPtr m_indexRegister; }; struct TryData { RefPtr