/*
- * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
+ * Copyright (C) 2008, 2009, 2012-2015 Apple Inc. All rights reserved.
* Copyright (C) 2008 Cameron Zwarich <cwzwarich@uwaterloo.ca>
+ * 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
* 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 Computer, Inc. ("Apple") nor the names of
+ * 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.
*
#define BytecodeGenerator_h
#include "CodeBlock.h"
-#include "HashTraits.h"
+#include <wtf/HashTraits.h>
#include "Instruction.h"
#include "Label.h"
#include "LabelScope.h"
#include "Interpreter.h"
+#include "ParserError.h"
#include "RegisterID.h"
#include "SymbolTable.h"
#include "Debugger.h"
#include "Nodes.h"
+#include "StaticPropertyAnalyzer.h"
+#include "TemplateRegistryKey.h"
+#include "UnlinkedCodeBlock.h"
+
+#include <functional>
+
#include <wtf/PassRefPtr.h>
#include <wtf/SegmentedVector.h>
#include <wtf/Vector.h>
+
namespace JSC {
class Identifier;
- class ScopeChainNode;
+ class JSTemplateRegistryKey;
+
+ enum ExpectedFunction {
+ NoExpectedFunction,
+ ExpectObjectConstructor,
+ ExpectArrayConstructor
+ };
class CallArguments {
public:
- CallArguments(BytecodeGenerator& generator, ArgumentsNode* argumentsNode);
+ CallArguments(BytecodeGenerator&, ArgumentsNode*, unsigned additionalArguments = 0);
RegisterID* thisRegister() { return m_argv[0].get(); }
RegisterID* argumentRegister(unsigned i) { return m_argv[i + 1].get(); }
- unsigned callFrame() { return thisRegister()->index() + count() + RegisterFile::CallFrameHeaderSize; }
- unsigned count() { return m_argv.size(); }
+ unsigned stackOffset() { return -m_argv[0]->index() + JSStack::CallFrameHeaderSize; }
+ unsigned argumentCountIncludingThis() { return m_argv.size() - m_padding; }
RegisterID* profileHookRegister() { return m_profileHookRegister.get(); }
ArgumentsNode* argumentsNode() { return m_argumentsNode; }
private:
RefPtr<RegisterID> m_profileHookRegister;
ArgumentsNode* m_argumentsNode;
- Vector<RefPtr<RegisterID>, 16> m_argv;
+ Vector<RefPtr<RegisterID>, 8, UnsafeVectorOverflow> m_argv;
+ unsigned m_padding;
};
struct FinallyContext {
- Label* finallyAddr;
- RegisterID* retAddrDst;
+ StatementNode* finallyBlock;
+ RegisterID* iterator;
+ ThrowableExpressionData* enumerationNode;
+ unsigned scopeContextStackSize;
+ unsigned switchContextStackSize;
+ unsigned forInContextStackSize;
+ unsigned tryContextStackSize;
+ unsigned labelScopesSize;
+ int finallyDepth;
+ int dynamicScopeDepth;
};
struct ControlFlowContext {
FinallyContext finallyContext;
};
- struct ForInContext {
- RefPtr<RegisterID> expectedSubscriptRegister;
- RefPtr<RegisterID> iterRegister;
- RefPtr<RegisterID> indexRegister;
- RefPtr<RegisterID> propertyRegister;
+ class ForInContext {
+ 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<RegisterID> m_localRegister;
+ bool m_isValid;
};
- class BytecodeGenerator {
- WTF_MAKE_FAST_ALLOCATED;
+ class StructureForInContext : public ForInContext {
public:
- typedef DeclarationStacks::VarStack VarStack;
- typedef DeclarationStacks::FunctionStack FunctionStack;
+ StructureForInContext(RegisterID* localRegister, RegisterID* indexRegister, RegisterID* propertyRegister, RegisterID* enumeratorRegister)
+ : ForInContext(localRegister)
+ , m_indexRegister(indexRegister)
+ , m_propertyRegister(propertyRegister)
+ , m_enumeratorRegister(enumeratorRegister)
+ {
+ }
- static void setDumpsGeneratedCode(bool dumpsGeneratedCode);
- static bool dumpsGeneratedCode();
+ virtual ForInContextType type() const
+ {
+ return StructureForInContextType;
+ }
- BytecodeGenerator(ProgramNode*, ScopeChainNode*, SymbolTable*, ProgramCodeBlock*);
- BytecodeGenerator(FunctionBodyNode*, ScopeChainNode*, SymbolTable*, CodeBlock*);
- BytecodeGenerator(EvalNode*, ScopeChainNode*, SymbolTable*, EvalCodeBlock*);
+ RegisterID* index() const { return m_indexRegister.get(); }
+ RegisterID* property() const { return m_propertyRegister.get(); }
+ RegisterID* enumerator() const { return m_enumeratorRegister.get(); }
- JSGlobalData* globalData() const { return m_globalData; }
- const CommonIdentifiers& propertyNames() const { return *m_globalData->propertyNames; }
+ private:
+ RefPtr<RegisterID> m_indexRegister;
+ RefPtr<RegisterID> m_propertyRegister;
+ RefPtr<RegisterID> m_enumeratorRegister;
+ };
- bool isConstructor() { return m_codeBlock->m_isConstructor; }
+ class IndexedForInContext : public ForInContext {
+ public:
+ IndexedForInContext(RegisterID* localRegister, RegisterID* indexRegister)
+ : ForInContext(localRegister)
+ , m_indexRegister(indexRegister)
+ {
+ }
- JSObject* generate();
+ virtual ForInContextType type() const
+ {
+ return IndexedForInContextType;
+ }
- // Returns the register corresponding to a local variable, or 0 if no
- // such register exists. Registers returned by registerFor do not
- // require explicit reference counting.
- RegisterID* registerFor(const Identifier&);
+ RegisterID* index() const { return m_indexRegister.get(); }
- // Returns the agument number if this is an argument, or 0 if not.
- int argumentNumberFor(const Identifier&);
+ private:
+ RefPtr<RegisterID> m_indexRegister;
+ };
- void setIsNumericCompareFunction(bool isNumericCompareFunction);
+ struct TryData {
+ RefPtr<Label> target;
+ unsigned targetScopeDepth;
+ HandlerType handlerType;
+ };
- bool willResolveToArguments(const Identifier&);
- RegisterID* uncheckedRegisterForArguments();
+ struct TryContext {
+ RefPtr<Label> start;
+ TryData* tryData;
+ };
- // Behaves as registerFor does, but ignores dynamic scope as
- // dynamic scope should not interfere with const initialisation
- RegisterID* constRegisterFor(const Identifier&);
+ class Variable {
+ public:
+ enum VariableKind { NormalVariable, SpecialVariable };
- // Searches the scope chain in an attempt to statically locate the requested
- // property. Returns false if for any reason the property cannot be safely
- // optimised at all. Otherwise it will return the index and depth of the
- // VariableObject that defines the property. If the property cannot be found
- // statically, depth will contain the depth of the scope chain where dynamic
- // lookup must begin.
- bool findScopedProperty(const Identifier&, int& index, size_t& depth, bool forWriting, bool& includesDynamicScopes, JSObject*& globalObject);
+ Variable()
+ : m_offset()
+ , m_local(nullptr)
+ , m_attributes(0)
+ , m_kind(NormalVariable)
+ {
+ }
+
+ Variable(const Identifier& ident)
+ : m_ident(ident)
+ , m_local(nullptr)
+ , m_attributes(0)
+ , m_kind(NormalVariable) // This is somewhat meaningless here for this kind of Variable.
+ {
+ }
+ Variable(const Identifier& ident, VarOffset offset, RegisterID* local, unsigned attributes, VariableKind kind)
+ : m_ident(ident)
+ , m_offset(offset)
+ , m_local(local)
+ , m_attributes(attributes)
+ , m_kind(kind)
+ {
+ }
+
+ // If it's unset, then it is a non-locally-scoped variable. If it is set, then it could be
+ // a stack variable, a scoped variable in the local scope, or a variable captured in the
+ // direct arguments object.
+ bool isResolved() const { return !!m_offset; }
+
+ const Identifier& ident() const { return m_ident; }
+
+ VarOffset offset() const { return m_offset; }
+ bool isLocal() const { return m_offset.isStack(); }
+ RegisterID* local() const { return m_local; }
+
+ bool isReadOnly() const { return m_attributes & ReadOnly; }
+ bool isSpecial() const { return m_kind != NormalVariable; }
+
+ private:
+ Identifier m_ident;
+ VarOffset m_offset;
+ RegisterID* m_local;
+ unsigned m_attributes;
+ VariableKind m_kind;
+ };
+
+ struct TryRange {
+ RefPtr<Label> start;
+ RefPtr<Label> end;
+ TryData* tryData;
+ };
+
+ enum ProfileTypeBytecodeFlag {
+ ProfileTypeBytecodePutToScope,
+ ProfileTypeBytecodeGetFromScope,
+ ProfileTypeBytecodePutToLocalScope,
+ ProfileTypeBytecodeGetFromLocalScope,
+ ProfileTypeBytecodeHasGlobalID,
+ ProfileTypeBytecodeDoesNotHaveGlobalID,
+ ProfileTypeBytecodeFunctionArgument,
+ ProfileTypeBytecodeFunctionReturnStatement
+ };
+
+ class BytecodeGenerator {
+ WTF_MAKE_FAST_ALLOCATED;
+ WTF_MAKE_NONCOPYABLE(BytecodeGenerator);
+ public:
+ typedef DeclarationStacks::VarStack VarStack;
+ typedef DeclarationStacks::FunctionStack FunctionStack;
+
+ BytecodeGenerator(VM&, ProgramNode*, UnlinkedProgramCodeBlock*, DebuggerMode, ProfilerMode);
+ BytecodeGenerator(VM&, FunctionNode*, UnlinkedFunctionCodeBlock*, DebuggerMode, ProfilerMode);
+ BytecodeGenerator(VM&, EvalNode*, UnlinkedEvalCodeBlock*, DebuggerMode, ProfilerMode);
+
+ ~BytecodeGenerator();
+
+ VM* vm() const { return m_vm; }
+ ParserArena& parserArena() const { return m_scopeNode->parserArena(); }
+ const CommonIdentifiers& propertyNames() const { return *m_vm->propertyNames; }
+
+ bool isConstructor() const { return m_codeBlock->isConstructor(); }
+#if ENABLE(ES6_CLASS_SYNTAX)
+ ConstructorKind constructorKind() const { return m_codeBlock->constructorKind(); }
+#else
+ ConstructorKind constructorKind() const { return ConstructorKind::None; }
+#endif
+
+ ParserError generate();
+
+ bool isArgumentNumber(const Identifier&, int);
+
+ Variable variable(const Identifier&);
+
+ // Ignores the possibility of intervening scopes.
+ Variable variablePerSymbolTable(const Identifier&);
+
+ enum ExistingVariableMode { VerifyExisting, IgnoreExisting };
+ void createVariable(const Identifier&, VarKind, ConstantMode, ExistingVariableMode = VerifyExisting); // Creates the variable, or asserts that the already-created variable is sufficiently compatible.
+
// Returns the register storing "this"
RegisterID* thisRegister() { return &m_thisRegister; }
+ RegisterID* argumentsRegister() { return m_argumentsRegister; }
+ RegisterID* newTarget() { return m_newTargetRegister; }
- bool isLocal(const Identifier&);
- bool isLocalConstant(const Identifier&);
+ RegisterID* scopeRegister() { return m_scopeRegister; }
// Returns the next available temporary register. Registers returned by
// newTemporary require a modified form of reference counting: any
// the next instruction may overwrite it.
RegisterID* newTemporary();
- RegisterID* highestUsedRegister();
-
// The same as newTemporary(), but this function returns "suggestion" if
// "suggestion" is a temporary. This function is helpful in situations
// where you've put "suggestion" in a RefPtr, but you'd like to allow
return newTemporary();
}
- // Returns the place to write the final output of an operation.
- RegisterID* finalDestinationOrIgnored(RegisterID* originalDst, RegisterID* tempDst = 0)
- {
- if (originalDst)
- return originalDst;
- ASSERT(tempDst != ignoredResult());
- if (tempDst && tempDst->isTemporary())
- return tempDst;
- return newTemporary();
- }
-
RegisterID* destinationForAssignResult(RegisterID* dst)
{
if (dst && dst != ignoredResult() && m_codeBlock->needsFullScopeChain())
return dst == ignoredResult() ? 0 : (dst && dst != src) ? emitMove(dst, src) : src;
}
- PassRefPtr<LabelScope> newLabelScope(LabelScope::Type, const Identifier* = 0);
+ LabelScopePtr newLabelScope(LabelScope::Type, const Identifier* = 0);
PassRefPtr<Label> newLabel();
- // The emitNode functions are just syntactic sugar for calling
- // Node::emitCode. These functions accept a 0 for the register,
- // meaning that the node should allocate a register, or ignoredResult(),
- // meaning that the node need not put the result in a register.
- // Other emit functions do not accept 0 or ignoredResult().
- RegisterID* emitNode(RegisterID* dst, Node* n)
+ void emitNode(RegisterID* dst, StatementNode* n)
{
// Node::emitCode assumes that dst, if provided, is either a local or a referenced temporary.
ASSERT(!dst || dst == ignoredResult() || !dst->isTemporary() || dst->refCount());
- addLineInfo(n->lineNo());
- return m_stack.recursionCheck()
- ? n->emitBytecode(*this, dst)
- : emitThrowExpressionTooDeepException();
+ if (!m_vm->isSafeToRecurse()) {
+ emitThrowExpressionTooDeepException();
+ return;
+ }
+ n->emitBytecode(*this, dst);
+ }
+
+ void emitNode(StatementNode* n)
+ {
+ emitNode(0, n);
}
- RegisterID* emitNode(Node* n)
+ RegisterID* emitNode(RegisterID* dst, ExpressionNode* n)
+ {
+ // Node::emitCode assumes that dst, if provided, is either a local or a referenced temporary.
+ ASSERT(!dst || dst == ignoredResult() || !dst->isTemporary() || dst->refCount());
+ if (!m_vm->isSafeToRecurse())
+ return emitThrowExpressionTooDeepException();
+ return n->emitBytecode(*this, dst);
+ }
+
+ RegisterID* emitNode(ExpressionNode* n)
{
return emitNode(0, n);
}
- void emitNodeInConditionContext(ExpressionNode* n, Label* trueTarget, Label* falseTarget, bool fallThroughMeansTrue)
+ void emitNodeInConditionContext(ExpressionNode* n, Label* trueTarget, Label* falseTarget, FallThroughMode fallThroughMode)
{
- addLineInfo(n->lineNo());
- if (m_stack.recursionCheck())
- n->emitBytecodeInConditionContext(*this, trueTarget, falseTarget, fallThroughMeansTrue);
- else
+ if (!m_vm->isSafeToRecurse()) {
emitThrowExpressionTooDeepException();
+ return;
+ }
+
+ n->emitBytecodeInConditionContext(*this, trueTarget, falseTarget, fallThroughMode);
}
- void emitExpressionInfo(unsigned divot, unsigned startOffset, unsigned endOffset)
- {
- if (!m_shouldEmitRichSourceInfo)
+ void emitExpressionInfo(const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd)
+ {
+ ASSERT(divot.offset >= divotStart.offset);
+ ASSERT(divotEnd.offset >= divot.offset);
+
+ int sourceOffset = m_scopeNode->source().startOffset();
+ unsigned firstLine = m_scopeNode->source().firstLine();
+
+ int divotOffset = divot.offset - sourceOffset;
+ int startOffset = divot.offset - divotStart.offset;
+ int endOffset = divotEnd.offset - divot.offset;
+
+ unsigned line = divot.line;
+ ASSERT(line >= firstLine);
+ line -= firstLine;
+
+ int lineStart = divot.lineStartOffset;
+ if (lineStart > sourceOffset)
+ lineStart -= sourceOffset;
+ else
+ lineStart = 0;
+
+ if (divotOffset < lineStart)
return;
- divot -= m_codeBlock->sourceOffset();
- if (divot > ExpressionRangeInfo::MaxDivot) {
- // Overflow has occurred, we can only give line number info for errors for this region
- divot = 0;
- startOffset = 0;
- endOffset = 0;
- } else if (startOffset > ExpressionRangeInfo::MaxOffset) {
- // If the start offset is out of bounds we clear both offsets
- // so we only get the divot marker. Error message will have to be reduced
- // to line and column number.
- startOffset = 0;
- endOffset = 0;
- } else if (endOffset > ExpressionRangeInfo::MaxOffset) {
- // The end offset is only used for additional context, and is much more likely
- // to overflow (eg. function call arguments) so we are willing to drop it without
- // dropping the rest of the range.
- endOffset = 0;
- }
-
- ExpressionRangeInfo info;
- info.instructionOffset = instructions().size();
- info.divotPoint = divot;
- info.startOffset = startOffset;
- info.endOffset = endOffset;
- m_codeBlock->addExpressionInfo(info);
+ unsigned column = divotOffset - lineStart;
+
+ unsigned instructionOffset = instructions().size();
+ if (!m_isBuiltinFunction)
+ m_codeBlock->addExpressionInfo(instructionOffset, divotOffset, startOffset, endOffset, line, column);
}
+
ALWAYS_INLINE bool leftHandSideNeedsCopy(bool rightHasAssignments, bool rightIsPure)
{
return (m_codeType != FunctionCode || m_codeBlock->needsFullScopeChain() || rightHasAssignments) && !rightIsPure;
return dst;
}
- return PassRefPtr<RegisterID>(emitNode(n));
+ return emitNode(n);
}
+ void emitTypeProfilerExpressionInfo(const JSTextPosition& startDivot, const JSTextPosition& endDivot);
+ void emitProfileType(RegisterID* registerToProfile, ProfileTypeBytecodeFlag, const Identifier*);
+
+ void emitProfileControlFlow(int);
+
RegisterID* emitLoad(RegisterID* dst, bool);
- RegisterID* emitLoad(RegisterID* dst, double);
RegisterID* emitLoad(RegisterID* dst, const Identifier&);
- RegisterID* emitLoad(RegisterID* dst, JSValue);
+ RegisterID* emitLoad(RegisterID* dst, JSValue, SourceCodeRepresentation = SourceCodeRepresentation::Other);
+ RegisterID* emitLoadGlobalObject(RegisterID* dst);
RegisterID* emitUnaryOp(OpcodeID, RegisterID* dst, RegisterID* src);
RegisterID* emitBinaryOp(OpcodeID, RegisterID* dst, RegisterID* src1, RegisterID* src2, OperandTypes);
RegisterID* emitEqualityOp(OpcodeID, RegisterID* dst, RegisterID* src1, RegisterID* src2);
RegisterID* emitUnaryNoDstOp(OpcodeID, RegisterID* src);
+ RegisterID* emitCreateThis(RegisterID* dst);
+ void emitTDZCheck(RegisterID* target);
RegisterID* emitNewObject(RegisterID* dst);
RegisterID* emitNewArray(RegisterID* dst, ElementNode*, unsigned length); // stops at first elision
- RegisterID* emitNewFunction(RegisterID* dst, FunctionBodyNode* body);
- RegisterID* emitLazyNewFunction(RegisterID* dst, FunctionBodyNode* body);
- RegisterID* emitNewFunctionInternal(RegisterID* dst, unsigned index, bool shouldNullCheck);
+ RegisterID* emitNewFunction(RegisterID* dst, FunctionBodyNode*);
+ RegisterID* emitNewFunctionInternal(RegisterID* dst, unsigned index);
RegisterID* emitNewFunctionExpression(RegisterID* dst, FuncExprNode* func);
+ RegisterID* emitNewDefaultConstructor(RegisterID* dst, ConstructorKind, const Identifier& name);
RegisterID* emitNewRegExp(RegisterID* dst, RegExp*);
+ RegisterID* emitMoveLinkTimeConstant(RegisterID* dst, LinkTimeConstant);
+ RegisterID* emitMoveEmptyValue(RegisterID* dst);
RegisterID* emitMove(RegisterID* dst, RegisterID* src);
- RegisterID* emitToJSNumber(RegisterID* dst, RegisterID* src) { return emitUnaryOp(op_to_jsnumber, dst, src); }
- RegisterID* emitPreInc(RegisterID* srcDst);
- RegisterID* emitPreDec(RegisterID* srcDst);
- RegisterID* emitPostInc(RegisterID* dst, RegisterID* srcDst);
- RegisterID* emitPostDec(RegisterID* dst, RegisterID* srcDst);
+ RegisterID* emitToNumber(RegisterID* dst, RegisterID* src) { return emitUnaryOp(op_to_number, dst, src); }
+ RegisterID* emitToString(RegisterID* dst, RegisterID* src) { return emitUnaryOp(op_to_string, dst, src); }
+ RegisterID* emitInc(RegisterID* srcDst);
+ RegisterID* emitDec(RegisterID* srcDst);
- void emitCheckHasInstance(RegisterID* base);
- RegisterID* emitInstanceOf(RegisterID* dst, RegisterID* value, RegisterID* base, RegisterID* basePrototype);
+ void emitCheckHasInstance(RegisterID* dst, RegisterID* value, RegisterID* base, Label* target);
+ RegisterID* emitInstanceOf(RegisterID* dst, RegisterID* value, RegisterID* basePrototype);
RegisterID* emitTypeOf(RegisterID* dst, RegisterID* src) { return emitUnaryOp(op_typeof, dst, src); }
RegisterID* emitIn(RegisterID* dst, RegisterID* property, RegisterID* base) { return emitBinaryOp(op_in, dst, property, base, OperandTypes()); }
- RegisterID* emitResolve(RegisterID* dst, const Identifier& property);
- RegisterID* emitGetScopedVar(RegisterID* dst, size_t skip, int index, JSValue globalObject);
- RegisterID* emitPutScopedVar(size_t skip, int index, RegisterID* value, JSValue globalObject);
-
- RegisterID* emitResolveBase(RegisterID* dst, const Identifier& property);
- RegisterID* emitResolveBaseForPut(RegisterID* dst, const Identifier& property);
- RegisterID* emitResolveWithBase(RegisterID* baseDst, RegisterID* propDst, const Identifier& property);
-
- void emitMethodCheck();
+ RegisterID* emitInitGlobalConst(const Identifier&, RegisterID* value);
RegisterID* emitGetById(RegisterID* dst, RegisterID* base, const Identifier& property);
- RegisterID* emitGetArgumentsLength(RegisterID* dst, RegisterID* base);
RegisterID* emitPutById(RegisterID* base, const Identifier& property, RegisterID* value);
- RegisterID* emitDirectPutById(RegisterID* base, const Identifier& property, RegisterID* value);
+ RegisterID* emitDirectPutById(RegisterID* base, const Identifier& property, RegisterID* value, PropertyNode::PutType);
RegisterID* emitDeleteById(RegisterID* dst, RegisterID* base, const Identifier&);
RegisterID* emitGetByVal(RegisterID* dst, RegisterID* base, RegisterID* property);
RegisterID* emitGetArgumentByVal(RegisterID* dst, RegisterID* base, RegisterID* property);
RegisterID* emitPutByVal(RegisterID* base, RegisterID* property, RegisterID* value);
+ RegisterID* emitDirectPutByVal(RegisterID* base, RegisterID* property, RegisterID* value);
RegisterID* emitDeleteByVal(RegisterID* dst, RegisterID* base, RegisterID* property);
RegisterID* emitPutByIndex(RegisterID* base, unsigned index, RegisterID* value);
- RegisterID* emitPutGetter(RegisterID* base, const Identifier& property, RegisterID* value);
- RegisterID* emitPutSetter(RegisterID* base, const Identifier& property, RegisterID* value);
- RegisterID* emitCall(RegisterID* dst, RegisterID* func, CallArguments&, unsigned divot, unsigned startOffset, unsigned endOffset);
- RegisterID* emitCallEval(RegisterID* dst, RegisterID* func, CallArguments&, unsigned divot, unsigned startOffset, unsigned endOffset);
- RegisterID* emitCallVarargs(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* argCount, unsigned divot, unsigned startOffset, unsigned endOffset);
- RegisterID* emitLoadVarargs(RegisterID* argCountDst, RegisterID* thisRegister, RegisterID* args);
+ void emitPutGetterById(RegisterID* base, const Identifier& property, RegisterID* getter);
+ void emitPutSetterById(RegisterID* base, const Identifier& property, RegisterID* setter);
+ void emitPutGetterSetter(RegisterID* base, const Identifier& property, RegisterID* getter, RegisterID* setter);
+
+ ExpectedFunction expectedFunctionForIdentifier(const Identifier&);
+ RegisterID* emitCall(RegisterID* dst, RegisterID* func, ExpectedFunction, CallArguments&, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd);
+ RegisterID* emitCallEval(RegisterID* dst, RegisterID* func, CallArguments&, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd);
+ RegisterID* emitCallVarargs(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* arguments, RegisterID* firstFreeRegister, int32_t firstVarArgOffset, RegisterID* profileHookRegister, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd);
+
+ enum PropertyDescriptorOption {
+ PropertyConfigurable = 1,
+ PropertyWritable = 1 << 1,
+ PropertyEnumerable = 1 << 2,
+ };
+ void emitCallDefineProperty(RegisterID* newObj, RegisterID* propertyNameRegister,
+ RegisterID* valueRegister, RegisterID* getterRegister, RegisterID* setterRegister, unsigned options, const JSTextPosition&);
+
+ void emitEnumeration(ThrowableExpressionData* enumerationNode, ExpressionNode* subjectNode, const std::function<void(BytecodeGenerator&, RegisterID*)>& callBack);
+
+#if ENABLE(ES6_TEMPLATE_LITERAL_SYNTAX)
+ RegisterID* emitGetTemplateObject(RegisterID* dst, TaggedTemplateNode*);
+#endif
RegisterID* emitReturn(RegisterID* src);
RegisterID* emitEnd(RegisterID* src) { return emitUnaryNoDstOp(op_end, src); }
- RegisterID* emitConstruct(RegisterID* dst, RegisterID* func, CallArguments&, unsigned divot, unsigned startOffset, unsigned endOffset);
+ RegisterID* emitConstruct(RegisterID* dst, RegisterID* func, ExpectedFunction, CallArguments&, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd);
RegisterID* emitStrcat(RegisterID* dst, RegisterID* src, int count);
void emitToPrimitive(RegisterID* dst, RegisterID* src);
+ ResolveType resolveType();
+ RegisterID* emitResolveConstantLocal(RegisterID* dst, const Variable&);
+ RegisterID* emitResolveScope(RegisterID* dst, const Variable&);
+ RegisterID* emitGetFromScope(RegisterID* dst, RegisterID* scope, const Variable&, ResolveMode);
+ RegisterID* emitPutToScope(RegisterID* scope, const Variable&, RegisterID* value, ResolveMode);
+ RegisterID* initializeVariable(const Variable&, RegisterID* value);
+
PassRefPtr<Label> emitLabel(Label*);
+ void emitLoopHint();
PassRefPtr<Label> emitJump(Label* target);
PassRefPtr<Label> emitJumpIfTrue(RegisterID* cond, Label* target);
PassRefPtr<Label> emitJumpIfFalse(RegisterID* cond, Label* target);
PassRefPtr<Label> emitJumpIfNotFunctionCall(RegisterID* cond, Label* target);
PassRefPtr<Label> emitJumpIfNotFunctionApply(RegisterID* cond, Label* target);
- PassRefPtr<Label> emitJumpScopes(Label* target, int targetScopeDepth);
+ void emitPopScopes(RegisterID* srcDst, int targetScopeDepth);
+
+ RegisterID* emitHasIndexedProperty(RegisterID* dst, RegisterID* base, RegisterID* propertyName);
+ RegisterID* emitHasStructureProperty(RegisterID* dst, RegisterID* base, RegisterID* propertyName, RegisterID* enumerator);
+ RegisterID* emitHasGenericProperty(RegisterID* dst, RegisterID* base, RegisterID* propertyName);
+ RegisterID* emitGetPropertyEnumerator(RegisterID* dst, RegisterID* base);
+ RegisterID* emitGetEnumerableLength(RegisterID* dst, RegisterID* base);
+ RegisterID* emitGetStructurePropertyEnumerator(RegisterID* dst, RegisterID* base, RegisterID* length);
+ RegisterID* emitGetGenericPropertyEnumerator(RegisterID* dst, RegisterID* base, RegisterID* length, RegisterID* structureEnumerator);
+ RegisterID* emitEnumeratorStructurePropertyName(RegisterID* dst, RegisterID* enumerator, RegisterID* index);
+ RegisterID* emitEnumeratorGenericPropertyName(RegisterID* dst, RegisterID* enumerator, RegisterID* index);
+ RegisterID* emitToIndexString(RegisterID* dst, RegisterID* index);
+
+ RegisterID* emitIsObject(RegisterID* dst, RegisterID* src);
+ RegisterID* emitIsUndefined(RegisterID* dst, RegisterID* src);
+
+ RegisterID* emitIteratorNext(RegisterID* dst, RegisterID* iterator, const ThrowableExpressionData* node);
+ void emitIteratorClose(RegisterID* iterator, const ThrowableExpressionData* node);
- PassRefPtr<Label> emitJumpSubroutine(RegisterID* retAddrDst, Label*);
- void emitSubroutineReturn(RegisterID* retAddrSrc);
+ void emitReadOnlyExceptionIfNeeded();
- RegisterID* emitGetPropertyNames(RegisterID* dst, RegisterID* base, RegisterID* i, RegisterID* size, Label* breakTarget);
- RegisterID* emitNextPropertyName(RegisterID* dst, RegisterID* base, RegisterID* i, RegisterID* size, RegisterID* iter, Label* target);
+ // Start a try block. 'start' must have been emitted.
+ TryData* pushTry(Label* start);
+ // End a try block. 'end' must have been emitted.
+ void popTryAndEmitCatch(TryData*, RegisterID* exceptionRegister, RegisterID* thrownValueRegister, Label* end, HandlerType);
- RegisterID* emitCatch(RegisterID*, Label* start, Label* end);
void emitThrow(RegisterID* exc)
{
m_usesExceptions = true;
emitUnaryNoDstOp(op_throw, exc);
}
- void emitThrowReferenceError(const UString& message);
+ void emitThrowReferenceError(const String& message);
+ void emitThrowTypeError(const String& message);
- void emitPushNewScope(RegisterID* dst, const Identifier& property, RegisterID* value);
+ void emitPushFunctionNameScope(RegisterID* dst, const Identifier& property, RegisterID* value, unsigned attributes);
+ void emitPushCatchScope(RegisterID* dst, const Identifier& property, RegisterID* value, unsigned attributes);
- RegisterID* emitPushScope(RegisterID* scope);
- void emitPopScope();
+ void emitGetScope();
+ RegisterID* emitPushWithScope(RegisterID* dst, RegisterID* scope);
+ void emitPopScope(RegisterID* srcDst);
- void emitDebugHook(DebugHookID, int firstLine, int lastLine);
+ void emitDebugHook(DebugHookID, unsigned line, unsigned charOffset, unsigned lineStart);
- int scopeDepth() { return m_dynamicScopeDepth + m_finallyDepth; }
+ int scopeDepth() { return m_localScopeDepth + m_finallyDepth; }
bool hasFinaliser() { return m_finallyDepth != 0; }
- void pushFinallyContext(Label* target, RegisterID* returnAddrDst);
+ void pushFinallyContext(StatementNode* finallyBlock);
void popFinallyContext();
+ void pushIteratorCloseContext(RegisterID* iterator, ThrowableExpressionData* enumerationNode);
+ void popIteratorCloseContext();
- void pushOptimisedForIn(RegisterID* expectedBase, RegisterID* iter, RegisterID* index, RegisterID* propertyRegister)
- {
- ForInContext context = { expectedBase, iter, index, propertyRegister };
- m_forInContextStack.append(context);
- }
-
- void popOptimisedForIn()
- {
- m_forInContextStack.removeLast();
- }
+ void pushIndexedForInScope(RegisterID* local, RegisterID* index);
+ void popIndexedForInScope(RegisterID* local);
+ void pushStructureForInScope(RegisterID* local, RegisterID* index, RegisterID* property, RegisterID* enumerator);
+ void popStructureForInScope(RegisterID* local);
+ void invalidateForInContextForLocal(RegisterID* local);
- LabelScope* breakTarget(const Identifier&);
- LabelScope* continueTarget(const Identifier&);
+ LabelScopePtr breakTarget(const Identifier&);
+ LabelScopePtr continueTarget(const Identifier&);
void beginSwitch(RegisterID*, SwitchInfo::SwitchType);
void endSwitch(uint32_t clauseCount, RefPtr<Label>*, ExpressionNode**, Label* defaultLabel, int32_t min, int32_t range);
CodeType codeType() const { return m_codeType; }
bool shouldEmitProfileHooks() { return m_shouldEmitProfileHooks; }
+ bool shouldEmitDebugHooks() { return m_shouldEmitDebugHooks; }
bool isStrictMode() const { return m_codeBlock->isStrictMode(); }
+
+ bool isBuiltinFunction() const { return m_isBuiltinFunction; }
+
+ OpcodeID lastOpcodeID() const { return m_lastOpcodeID; }
private:
+ Variable variableForLocalEntry(const Identifier&, const SymbolTableEntry&);
+
void emitOpcode(OpcodeID);
+ UnlinkedArrayAllocationProfile newArrayAllocationProfile();
+ UnlinkedObjectAllocationProfile newObjectAllocationProfile();
+ UnlinkedArrayProfile newArrayProfile();
+ UnlinkedValueProfile emitProfiledOpcode(OpcodeID);
+ int kill(RegisterID* dst)
+ {
+ int index = dst->index();
+ m_staticPropertyAnalyzer.kill(index);
+ return index;
+ }
+
void retrieveLastBinaryOp(int& dstIndex, int& src1Index, int& src2Index);
void retrieveLastUnaryOp(int& dstIndex, int& srcIndex);
ALWAYS_INLINE void rewindBinaryOp();
ALWAYS_INLINE void rewindUnaryOp();
- PassRefPtr<Label> emitComplexJumpScopes(Label* target, ControlFlowContext* topScope, ControlFlowContext* bottomScope);
+ void allocateAndEmitScope();
+ void emitComplexPopScopes(RegisterID*, ControlFlowContext* topScope, ControlFlowContext* bottomScope);
- typedef HashMap<EncodedJSValue, unsigned, EncodedJSValueHash, EncodedJSValueHashTraits> JSValueMap;
-
- struct IdentifierMapIndexHashTraits {
- typedef int TraitType;
- typedef IdentifierMapIndexHashTraits StorageTraits;
- static int emptyValue() { return std::numeric_limits<int>::max(); }
- static const bool emptyValueIsZero = false;
- static const bool needsDestruction = false;
- static const bool needsRef = false;
- };
-
- typedef HashMap<RefPtr<StringImpl>, int, IdentifierRepHash, HashTraits<RefPtr<StringImpl> >, IdentifierMapIndexHashTraits> IdentifierMap;
typedef HashMap<double, JSValue> NumberMap;
- typedef HashMap<StringImpl*, JSString*, IdentifierRepHash> IdentifierStringMap;
+ typedef HashMap<UniquedStringImpl*, JSString*, IdentifierRepHash> IdentifierStringMap;
+ typedef HashMap<TemplateRegistryKey, JSTemplateRegistryKey*> TemplateRegistryKeyMap;
- RegisterID* emitCall(OpcodeID, RegisterID* dst, RegisterID* func, CallArguments&, unsigned divot, unsigned startOffset, unsigned endOffset);
+ // Helper for emitCall() and emitConstruct(). This works because the set of
+ // expected functions have identical behavior for both call and construct
+ // (i.e. "Object()" is identical to "new Object()").
+ ExpectedFunction emitExpectedFunctionSnippet(RegisterID* dst, RegisterID* func, ExpectedFunction, CallArguments&, Label* done);
+
+ RegisterID* emitCall(OpcodeID, RegisterID* dst, RegisterID* func, ExpectedFunction, CallArguments&, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd);
RegisterID* newRegister();
- // Adds a var slot and maps it to the name ident in symbolTable().
- RegisterID* addVar(const Identifier& ident, bool isConstant)
- {
- RegisterID* local;
- addVar(ident, isConstant, local);
- return local;
- }
-
- // Ditto. Returns true if a new RegisterID was added, false if a pre-existing RegisterID was re-used.
- bool addVar(const Identifier&, bool isConstant, RegisterID*&);
-
- // Adds an anonymous var slot. To give this slot a name, add it to symbolTable().
+ // Adds an anonymous local var slot. To give this slot a name, add it to symbolTable().
RegisterID* addVar()
{
++m_codeBlock->m_numVars;
- return newRegister();
- }
-
- // Returns the RegisterID corresponding to ident.
- RegisterID* addGlobalVar(const Identifier& ident, bool isConstant)
- {
- RegisterID* local;
- addGlobalVar(ident, isConstant, local);
- return local;
+ RegisterID* result = newRegister();
+ ASSERT(VirtualRegister(result->index()).toLocal() == m_codeBlock->m_numVars - 1);
+ result->ref(); // We should never free this slot.
+ return result;
}
- // Returns true if a new RegisterID was added, false if a pre-existing RegisterID was re-used.
- bool addGlobalVar(const Identifier&, bool isConstant, RegisterID*&);
- void addParameter(const Identifier&, int parameterIndex);
+ // Initializes the stack form the parameter; does nothing for the symbol table.
+ RegisterID* initializeNextParameter();
+ UniquedStringImpl* visibleNameForParameter(DestructuringPatternNode*);
- void preserveLastVar();
- bool shouldAvoidResolveGlobal();
-
- RegisterID& registerFor(int index)
+ RegisterID& registerFor(VirtualRegister reg)
{
- if (index >= 0)
- return m_calleeRegisters[index];
+ if (reg.isLocal())
+ return m_calleeRegisters[reg.toLocal()];
- if (m_parameters.size()) {
- ASSERT(!m_globals.size());
- return m_parameters[index + m_parameters.size() + RegisterFile::CallFrameHeaderSize];
- }
+ if (reg.offset() == JSStack::Callee)
+ return m_calleeRegister;
- return m_globals[-index - 1];
+ ASSERT(m_parameters.size());
+ return m_parameters[reg.toArgument()];
}
+ bool hasConstant(const Identifier&) const;
unsigned addConstant(const Identifier&);
- RegisterID* addConstantValue(JSValue);
+ RegisterID* addConstantValue(JSValue, SourceCodeRepresentation = SourceCodeRepresentation::Other);
+ RegisterID* addConstantEmptyValue();
unsigned addRegExp(RegExp*);
unsigned addConstantBuffer(unsigned length);
- FunctionExecutable* makeFunction(ExecState* exec, FunctionBodyNode* body)
+ UnlinkedFunctionExecutable* makeFunction(FunctionBodyNode* body)
{
- return FunctionExecutable::create(exec, body->ident(), body->source(), body->usesArguments(), body->parameters(), body->isStrictMode(), body->lineNo(), body->lastLine());
+ return UnlinkedFunctionExecutable::create(m_vm, m_scopeNode->source(), body, isBuiltinFunction() ? UnlinkedBuiltinFunction : UnlinkedNormalFunction);
}
- FunctionExecutable* makeFunction(JSGlobalData* globalData, FunctionBodyNode* body)
- {
- return FunctionExecutable::create(globalData, body->ident(), body->source(), body->usesArguments(), body->parameters(), body->isStrictMode(), body->lineNo(), body->lastLine());
- }
+ RegisterID* emitConstructVarargs(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* arguments, RegisterID* firstFreeRegister, int32_t firstVarArgOffset, RegisterID* profileHookRegister, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd);
+ RegisterID* emitCallVarargs(OpcodeID, RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* arguments, RegisterID* firstFreeRegister, int32_t firstVarArgOffset, RegisterID* profileHookRegister, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd);
+ public:
JSString* addStringConstant(const Identifier&);
+ JSTemplateRegistryKey* addTemplateRegistryKeyConstant(const TemplateRegistryKey&);
+
+ Vector<UnlinkedInstruction, 0, UnsafeVectorOverflow>& instructions() { return m_instructions; }
- void addLineInfo(unsigned lineNo)
+ SymbolTable& symbolTable() { return *m_symbolTable; }
+
+ bool shouldOptimizeLocals()
{
-#if !ENABLE(OPCODE_SAMPLING)
- if (m_shouldEmitRichSourceInfo)
-#endif
- m_codeBlock->addLineInfo(instructions().size(), lineNo);
+ if (m_codeType != FunctionCode)
+ return false;
+
+ if (m_localScopeDepth)
+ return false;
+
+ return true;
}
- RegisterID* emitInitLazyRegister(RegisterID*);
+ bool canOptimizeNonLocals()
+ {
+ if (m_localScopeDepth)
+ return false;
- Vector<Instruction>& instructions() { return m_codeBlock->instructions(); }
- SymbolTable& symbolTable() { return *m_symbolTable; }
+ if (m_codeType == EvalCode)
+ return false;
+
+ if (m_codeType == FunctionCode && m_codeBlock->usesEval())
+ return false;
- bool shouldOptimizeLocals() { return (m_codeType != EvalCode) && !m_dynamicScopeDepth; }
- bool canOptimizeNonLocals() { return (m_codeType == FunctionCode) && !m_dynamicScopeDepth && !m_codeBlock->usesEval(); }
+ return true;
+ }
RegisterID* emitThrowExpressionTooDeepException();
- void createArgumentsIfNecessary();
- void createActivationIfNecessary();
- RegisterID* createLazyRegisterIfNecessary(RegisterID*);
+ private:
+ Vector<UnlinkedInstruction, 0, UnsafeVectorOverflow> m_instructions;
bool m_shouldEmitDebugHooks;
bool m_shouldEmitProfileHooks;
- bool m_shouldEmitRichSourceInfo;
- Strong<ScopeChainNode> m_scopeChain;
- SymbolTable* m_symbolTable;
+ SymbolTable* m_symbolTable { nullptr };
- ScopeNode* m_scopeNode;
- CodeBlock* m_codeBlock;
+ ScopeNode* const m_scopeNode;
+ Strong<UnlinkedCodeBlock> m_codeBlock;
// Some of these objects keep pointers to one another. They are arranged
// to ensure a sane destruction order that avoids references to freed memory.
- HashSet<RefPtr<StringImpl>, IdentifierRepHash> m_functions;
+ HashSet<RefPtr<UniquedStringImpl>, IdentifierRepHash> m_functions;
RegisterID m_ignoredResultRegister;
RegisterID m_thisRegister;
- RegisterID* m_activationRegister;
+ RegisterID m_calleeRegister;
+ RegisterID* m_scopeRegister { nullptr };
+ RegisterID* m_argumentsRegister { nullptr };
+ RegisterID* m_lexicalEnvironmentRegister { nullptr };
+ RegisterID* m_emptyValueRegister { nullptr };
+ RegisterID* m_globalObjectRegister { nullptr };
+ RegisterID* m_newTargetRegister { nullptr };
+ RegisterID* m_linkTimeConstantRegisters[LinkTimeConstantCount];
+
SegmentedVector<RegisterID, 32> m_constantPoolRegisters;
SegmentedVector<RegisterID, 32> m_calleeRegisters;
SegmentedVector<RegisterID, 32> m_parameters;
- SegmentedVector<RegisterID, 32> m_globals;
SegmentedVector<Label, 32> m_labels;
- SegmentedVector<LabelScope, 8> m_labelScopes;
- RefPtr<RegisterID> m_lastVar;
- int m_finallyDepth;
- int m_dynamicScopeDepth;
- int m_baseScopeDepth;
- CodeType m_codeType;
-
- Vector<ControlFlowContext> m_scopeContextStack;
- Vector<SwitchInfo> m_switchContextStack;
- Vector<ForInContext> m_forInContextStack;
+ LabelScopeStore m_labelScopes;
+ int m_finallyDepth { 0 };
+ int m_localScopeDepth { 0 };
+ const CodeType m_codeType;
- int m_nextGlobalIndex;
- int m_firstConstantIndex;
- int m_nextConstantOffset;
- unsigned m_globalConstantIndex;
+ Vector<ControlFlowContext, 0, UnsafeVectorOverflow> m_scopeContextStack;
+ Vector<SwitchInfo> m_switchContextStack;
+ Vector<std::unique_ptr<ForInContext>> m_forInContextStack;
+ Vector<TryContext> m_tryContextStack;
+ Vector<std::pair<RefPtr<RegisterID>, const DestructuringPatternNode*>> m_destructuringParameters;
+ enum FunctionVariableType : uint8_t { NormalFunctionVariable, GlobalFunctionVariable };
+ Vector<std::pair<FunctionBodyNode*, FunctionVariableType>> m_functionsToInitialize;
+ bool m_needToInitializeArguments { false };
+
+ Vector<TryRange> m_tryRanges;
+ SegmentedVector<TryData, 8> m_tryData;
- int m_globalVarStorageOffset;
+ int m_nextConstantOffset { 0 };
- bool m_hasCreatedActivation;
- int m_firstLazyFunction;
- int m_lastLazyFunction;
- HashMap<unsigned int, FunctionBodyNode*, WTF::IntHash<unsigned int>, WTF::UnsignedWithZeroKeyHashTraits<unsigned int> > m_lazyFunctions;
typedef HashMap<FunctionBodyNode*, unsigned> FunctionOffsetMap;
FunctionOffsetMap m_functionOffsets;
// Constant pool
IdentifierMap m_identifierMap;
+
+ typedef HashMap<EncodedJSValueWithRepresentation, unsigned, EncodedJSValueWithRepresentationHash, EncodedJSValueWithRepresentationHashTraits> JSValueMap;
JSValueMap m_jsValueMap;
- NumberMap m_numberMap;
IdentifierStringMap m_stringMap;
+ TemplateRegistryKeyMap m_templateRegistryKeyMap;
+
+ StaticPropertyAnalyzer m_staticPropertyAnalyzer { &m_instructions };
- JSGlobalData* m_globalData;
+ VM* m_vm;
- OpcodeID m_lastOpcodeID;
+ OpcodeID m_lastOpcodeID = op_end;
#ifndef NDEBUG
- size_t m_lastOpcodePosition;
+ size_t m_lastOpcodePosition { 0 };
#endif
- StackBounds m_stack;
-
- bool m_usesExceptions;
- bool m_expressionTooDeep;
+ bool m_usesExceptions { false };
+ bool m_expressionTooDeep { false };
+ bool m_isBuiltinFunction { false };
};
+
}
#endif // BytecodeGenerator_h