BytecodeGenerator.h [plain text]
#ifndef BytecodeGenerator_h
#define BytecodeGenerator_h
#include "CodeBlock.h"
#include "HashTraits.h"
#include "Instruction.h"
#include "Label.h"
#include "LabelScope.h"
#include "Interpreter.h"
#include "RegisterID.h"
#include "SymbolTable.h"
#include "Debugger.h"
#include "Nodes.h"
#include <wtf/FastAllocBase.h>
#include <wtf/PassRefPtr.h>
#include <wtf/SegmentedVector.h>
#include <wtf/Vector.h>
namespace JSC {
class Identifier;
class ScopeChain;
class ScopeNode;
struct FinallyContext {
Label* finallyAddr;
RegisterID* retAddrDst;
};
struct ControlFlowContext {
bool isFinallyBlock;
FinallyContext finallyContext;
};
struct ForInContext {
RefPtr<RegisterID> expectedSubscriptRegister;
RefPtr<RegisterID> iterRegister;
RefPtr<RegisterID> indexRegister;
RefPtr<RegisterID> propertyRegister;
};
class BytecodeGenerator : public FastAllocBase {
public:
typedef DeclarationStacks::VarStack VarStack;
typedef DeclarationStacks::FunctionStack FunctionStack;
static void setDumpsGeneratedCode(bool dumpsGeneratedCode);
static bool dumpsGeneratedCode();
BytecodeGenerator(ProgramNode*, const Debugger*, const ScopeChain&, SymbolTable*, ProgramCodeBlock*);
BytecodeGenerator(FunctionBodyNode*, const Debugger*, const ScopeChain&, SymbolTable*, CodeBlock*);
BytecodeGenerator(EvalNode*, const Debugger*, const ScopeChain&, SymbolTable*, EvalCodeBlock*);
JSGlobalData* globalData() const { return m_globalData; }
const CommonIdentifiers& propertyNames() const { return *m_globalData->propertyNames; }
void generate();
RegisterID* registerFor(const Identifier&);
bool willResolveToArguments(const Identifier&);
RegisterID* uncheckedRegisterForArguments();
RegisterID* constRegisterFor(const Identifier&);
bool findScopedProperty(const Identifier&, int& index, size_t& depth, bool forWriting, bool& includesDynamicScopes, JSObject*& globalObject);
RegisterID* thisRegister() { return &m_thisRegister; }
bool isLocal(const Identifier&);
bool isLocalConstant(const Identifier&);
RegisterID* newTemporary();
RegisterID* highestUsedRegister();
RegisterID* newTemporaryOr(RegisterID* suggestion) { return suggestion->isTemporary() ? suggestion : newTemporary(); }
RegisterID* ignoredResult() { return &m_ignoredResultRegister; }
RegisterID* tempDestination(RegisterID* dst)
{
return (dst && dst != ignoredResult() && dst->isTemporary()) ? dst : newTemporary();
}
RegisterID* finalDestination(RegisterID* originalDst, RegisterID* tempDst = 0)
{
if (originalDst && originalDst != ignoredResult())
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->isTemporary() ? dst : newTemporary();
return 0;
}
RegisterID* moveToDestinationIfNeeded(RegisterID* dst, RegisterID* src)
{
return dst == ignoredResult() ? 0 : (dst && dst != src) ? emitMove(dst, src) : src;
}
PassRefPtr<LabelScope> newLabelScope(LabelScope::Type, const Identifier* = 0);
PassRefPtr<Label> newLabel();
RegisterID* emitNode(RegisterID* dst, Node* n)
{
ASSERT(!dst || dst == ignoredResult() || !dst->isTemporary() || dst->refCount());
if (!m_codeBlock->numberOfLineInfos() || m_codeBlock->lastLineInfo().lineNumber != n->lineNo()) {
LineInfo info = { instructions().size(), n->lineNo() };
m_codeBlock->addLineInfo(info);
}
if (m_emitNodeDepth >= s_maxEmitNodeDepth)
return emitThrowExpressionTooDeepException();
++m_emitNodeDepth;
RegisterID* r = n->emitBytecode(*this, dst);
--m_emitNodeDepth;
return r;
}
RegisterID* emitNode(Node* n)
{
return emitNode(0, n);
}
void emitNodeInConditionContext(ExpressionNode* n, Label* trueTarget, Label* falseTarget, bool fallThroughMeansTrue)
{
if (!m_codeBlock->numberOfLineInfos() || m_codeBlock->lastLineInfo().lineNumber != n->lineNo()) {
LineInfo info = { instructions().size(), n->lineNo() };
m_codeBlock->addLineInfo(info);
}
if (m_emitNodeDepth >= s_maxEmitNodeDepth)
emitThrowExpressionTooDeepException();
++m_emitNodeDepth;
n->emitBytecodeInConditionContext(*this, trueTarget, falseTarget, fallThroughMeansTrue);
--m_emitNodeDepth;
}
void emitExpressionInfo(unsigned divot, unsigned startOffset, unsigned endOffset)
{
divot -= m_codeBlock->sourceOffset();
if (divot > ExpressionRangeInfo::MaxDivot) {
divot = 0;
startOffset = 0;
endOffset = 0;
} else if (startOffset > ExpressionRangeInfo::MaxOffset) {
startOffset = 0;
endOffset = 0;
} else if (endOffset > ExpressionRangeInfo::MaxOffset) {
endOffset = 0;
}
ExpressionRangeInfo info;
info.instructionOffset = instructions().size();
info.divotPoint = divot;
info.startOffset = startOffset;
info.endOffset = endOffset;
m_codeBlock->addExpressionInfo(info);
}
void emitGetByIdExceptionInfo(OpcodeID opcodeID)
{
ASSERT(opcodeID == op_construct || opcodeID == op_instanceof);
GetByIdExceptionInfo info;
info.bytecodeOffset = instructions().size();
info.isOpConstruct = (opcodeID == op_construct);
m_codeBlock->addGetByIdExceptionInfo(info);
}
ALWAYS_INLINE bool leftHandSideNeedsCopy(bool rightHasAssignments, bool rightIsPure)
{
return (m_codeType != FunctionCode || m_codeBlock->needsFullScopeChain() || rightHasAssignments) && !rightIsPure;
}
ALWAYS_INLINE PassRefPtr<RegisterID> emitNodeForLeftHandSide(ExpressionNode* n, bool rightHasAssignments, bool rightIsPure)
{
if (leftHandSideNeedsCopy(rightHasAssignments, rightIsPure)) {
PassRefPtr<RegisterID> dst = newTemporary();
emitNode(dst.get(), n);
return dst;
}
return PassRefPtr<RegisterID>(emitNode(n));
}
RegisterID* emitLoad(RegisterID* dst, bool);
RegisterID* emitLoad(RegisterID* dst, double);
RegisterID* emitLoad(RegisterID* dst, const Identifier&);
RegisterID* emitLoad(RegisterID* dst, JSValue);
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* emitNewObject(RegisterID* dst);
RegisterID* emitNewArray(RegisterID* dst, ElementNode*);
RegisterID* emitNewFunction(RegisterID* dst, FunctionBodyNode* body);
RegisterID* emitNewFunctionExpression(RegisterID* dst, FuncExprNode* func);
RegisterID* emitNewRegExp(RegisterID* dst, RegExp* regExp);
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* emitInstanceOf(RegisterID* dst, RegisterID* value, RegisterID* base, 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* emitResolveWithBase(RegisterID* baseDst, RegisterID* propDst, const Identifier& property);
void emitMethodCheck();
RegisterID* emitGetById(RegisterID* dst, RegisterID* base, const Identifier& property);
RegisterID* emitPutById(RegisterID* base, const Identifier& property, RegisterID* value);
RegisterID* emitDirectPutById(RegisterID* base, const Identifier& property, RegisterID* value);
RegisterID* emitDeleteById(RegisterID* dst, RegisterID* base, const Identifier&);
RegisterID* emitGetByVal(RegisterID* dst, RegisterID* base, RegisterID* property);
RegisterID* emitPutByVal(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, RegisterID* thisRegister, ArgumentsNode*, unsigned divot, unsigned startOffset, unsigned endOffset);
RegisterID* emitCallEval(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, ArgumentsNode*, 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* args);
RegisterID* emitReturn(RegisterID* src);
RegisterID* emitEnd(RegisterID* src) { return emitUnaryNoDstOp(op_end, src); }
RegisterID* emitConstruct(RegisterID* dst, RegisterID* func, ArgumentsNode*, unsigned divot, unsigned startOffset, unsigned endOffset);
RegisterID* emitStrcat(RegisterID* dst, RegisterID* src, int count);
void emitToPrimitive(RegisterID* dst, RegisterID* src);
PassRefPtr<Label> emitLabel(Label*);
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);
PassRefPtr<Label> emitJumpSubroutine(RegisterID* retAddrDst, Label*);
void emitSubroutineReturn(RegisterID* retAddrSrc);
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);
RegisterID* emitCatch(RegisterID*, Label* start, Label* end);
void emitThrow(RegisterID* exc) { emitUnaryNoDstOp(op_throw, exc); }
RegisterID* emitNewError(RegisterID* dst, ErrorType type, JSValue message);
void emitPushNewScope(RegisterID* dst, const Identifier& property, RegisterID* value);
RegisterID* emitPushScope(RegisterID* scope);
void emitPopScope();
void emitDebugHook(DebugHookID, int firstLine, int lastLine);
int scopeDepth() { return m_dynamicScopeDepth + m_finallyDepth; }
bool hasFinaliser() { return m_finallyDepth != 0; }
void pushFinallyContext(Label* target, RegisterID* returnAddrDst);
void popFinallyContext();
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();
}
LabelScope* breakTarget(const Identifier&);
LabelScope* 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; }
void setRegeneratingForExceptionInfo(CodeBlock* originalCodeBlock)
{
m_regeneratingForExceptionInfo = true;
m_codeBlockBeingRegeneratedFrom = originalCodeBlock;
}
private:
void emitOpcode(OpcodeID);
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);
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<UString::Rep>, int, IdentifierRepHash, HashTraits<RefPtr<UString::Rep> >, IdentifierMapIndexHashTraits> IdentifierMap;
typedef HashMap<double, JSValue> NumberMap;
typedef HashMap<UString::Rep*, JSString*, IdentifierRepHash> IdentifierStringMap;
RegisterID* emitCall(OpcodeID, RegisterID* dst, RegisterID* func, RegisterID* thisRegister, ArgumentsNode*, unsigned divot, unsigned startOffset, unsigned endOffset);
RegisterID* newRegister();
RegisterID* addVar(const Identifier& ident, bool isConstant)
{
RegisterID* local;
addVar(ident, isConstant, local);
return local;
}
bool addVar(const Identifier&, bool isConstant, RegisterID*&);
RegisterID* addGlobalVar(const Identifier& ident, bool isConstant)
{
RegisterID* local;
addGlobalVar(ident, isConstant, local);
return local;
}
bool addGlobalVar(const Identifier&, bool isConstant, RegisterID*&);
RegisterID* addParameter(const Identifier&);
void preserveLastVar();
RegisterID& registerFor(int index)
{
if (index >= 0)
return m_calleeRegisters[index];
if (index == RegisterFile::OptionalCalleeArguments)
return m_argumentsRegister;
if (m_parameters.size()) {
ASSERT(!m_globals.size());
return m_parameters[index + m_parameters.size() + RegisterFile::CallFrameHeaderSize];
}
return m_globals[-index - 1];
}
unsigned addConstant(const Identifier&);
RegisterID* addConstantValue(JSValue);
unsigned addRegExp(RegExp*);
PassRefPtr<FunctionExecutable> makeFunction(ExecState* exec, FunctionBodyNode* body)
{
return FunctionExecutable::create(exec, body->ident(), body->source(), body->usesArguments(), body->parameters(), body->lineNo(), body->lastLine());
}
PassRefPtr<FunctionExecutable> makeFunction(JSGlobalData* globalData, FunctionBodyNode* body)
{
return FunctionExecutable::create(globalData, body->ident(), body->source(), body->usesArguments(), body->parameters(), body->lineNo(), body->lastLine());
}
Vector<Instruction>& instructions() { return m_codeBlock->instructions(); }
SymbolTable& symbolTable() { return *m_symbolTable; }
bool shouldOptimizeLocals() { return (m_codeType != EvalCode) && !m_dynamicScopeDepth; }
bool canOptimizeNonLocals() { return (m_codeType == FunctionCode) && !m_dynamicScopeDepth && !m_codeBlock->usesEval(); }
RegisterID* emitThrowExpressionTooDeepException();
void createArgumentsIfNecessary();
bool m_shouldEmitDebugHooks;
bool m_shouldEmitProfileHooks;
const ScopeChain* m_scopeChain;
SymbolTable* m_symbolTable;
ScopeNode* m_scopeNode;
CodeBlock* m_codeBlock;
HashSet<RefPtr<UString::Rep>, IdentifierRepHash> m_functions;
RegisterID m_ignoredResultRegister;
RegisterID m_thisRegister;
RegisterID m_argumentsRegister;
int m_activationRegisterIndex;
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;
int m_nextGlobalIndex;
int m_nextParameterIndex;
int m_firstConstantIndex;
int m_nextConstantOffset;
unsigned m_globalConstantIndex;
int m_globalVarStorageOffset;
IdentifierMap m_identifierMap;
JSValueMap m_jsValueMap;
NumberMap m_numberMap;
IdentifierStringMap m_stringMap;
JSGlobalData* m_globalData;
OpcodeID m_lastOpcodeID;
unsigned m_emitNodeDepth;
bool m_regeneratingForExceptionInfo;
CodeBlock* m_codeBlockBeingRegeneratedFrom;
static const unsigned s_maxEmitNodeDepth = 5000;
};
}
#endif // BytecodeGenerator_h