BytecodeGenerator.cpp [plain text]
#include "config.h"
#include "BytecodeGenerator.h"
#include "BatchedTransitionOptimizer.h"
#include "JSFunction.h"
#include "Interpreter.h"
#include "ScopeChain.h"
#include "UString.h"
using namespace std;
namespace JSC {
#ifndef NDEBUG
static bool s_dumpsGeneratedCode = false;
#endif
void BytecodeGenerator::setDumpsGeneratedCode(bool dumpsGeneratedCode)
{
#ifndef NDEBUG
s_dumpsGeneratedCode = dumpsGeneratedCode;
#else
UNUSED_PARAM(dumpsGeneratedCode);
#endif
}
bool BytecodeGenerator::dumpsGeneratedCode()
{
#ifndef NDEBUG
return s_dumpsGeneratedCode;
#else
return false;
#endif
}
JSObject* BytecodeGenerator::generate()
{
m_codeBlock->setThisRegister(m_thisRegister.index());
m_scopeNode->emitBytecode(*this);
#ifndef NDEBUG
m_codeBlock->setInstructionCount(m_codeBlock->instructions().size());
if (s_dumpsGeneratedCode)
m_codeBlock->dump(m_scopeChain->globalObject->globalExec());
#endif
if ((m_codeType == FunctionCode && !m_codeBlock->needsFullScopeChain() && !m_codeBlock->usesArguments()) || m_codeType == EvalCode)
symbolTable().clear();
m_codeBlock->shrinkToFit();
if (m_expressionTooDeep)
return createOutOfMemoryError(m_scopeChain->globalObject.get());
return 0;
}
bool BytecodeGenerator::addVar(const Identifier& ident, bool isConstant, RegisterID*& r0)
{
int index = m_calleeRegisters.size();
SymbolTableEntry newEntry(index, isConstant ? ReadOnly : 0);
pair<SymbolTable::iterator, bool> result = symbolTable().add(ident.impl(), newEntry);
if (!result.second) {
r0 = ®isterFor(result.first->second.getIndex());
return false;
}
r0 = addVar();
return true;
}
bool BytecodeGenerator::addGlobalVar(const Identifier& ident, bool isConstant, RegisterID*& r0)
{
int index = m_nextGlobalIndex;
SymbolTableEntry newEntry(index, isConstant ? ReadOnly : 0);
pair<SymbolTable::iterator, bool> result = symbolTable().add(ident.impl(), newEntry);
if (!result.second)
index = result.first->second.getIndex();
else {
--m_nextGlobalIndex;
m_globals.append(index + m_globalVarStorageOffset);
}
r0 = ®isterFor(index);
return result.second;
}
void BytecodeGenerator::preserveLastVar()
{
if ((m_firstConstantIndex = m_calleeRegisters.size()) != 0)
m_lastVar = &m_calleeRegisters.last();
}
BytecodeGenerator::BytecodeGenerator(ProgramNode* programNode, ScopeChainNode* scopeChain, SymbolTable* symbolTable, ProgramCodeBlock* codeBlock)
: m_shouldEmitDebugHooks(scopeChain->globalObject->debugger())
, m_shouldEmitProfileHooks(scopeChain->globalObject->supportsProfiling())
, m_shouldEmitRichSourceInfo(scopeChain->globalObject->supportsRichSourceInfo())
, m_scopeChain(*scopeChain->globalData, scopeChain)
, m_symbolTable(symbolTable)
, m_scopeNode(programNode)
, m_codeBlock(codeBlock)
, m_thisRegister(RegisterFile::ProgramCodeThisRegister)
, m_finallyDepth(0)
, m_dynamicScopeDepth(0)
, m_baseScopeDepth(0)
, m_codeType(GlobalCode)
, m_nextGlobalIndex(-1)
, m_nextConstantOffset(0)
, m_globalConstantIndex(0)
, m_hasCreatedActivation(true)
, m_firstLazyFunction(0)
, m_lastLazyFunction(0)
, m_globalData(scopeChain->globalData)
, m_lastOpcodeID(op_end)
#ifndef NDEBUG
, m_lastOpcodePosition(0)
#endif
, m_stack(m_globalData->stack())
, m_usesExceptions(false)
, m_expressionTooDeep(false)
{
if (m_shouldEmitDebugHooks)
m_codeBlock->setNeedsFullScopeChain(true);
emitOpcode(op_enter);
codeBlock->setGlobalData(m_globalData);
m_codeBlock->m_numParameters = 1;
JSGlobalObject* globalObject = scopeChain->globalObject.get();
ExecState* exec = globalObject->globalExec();
RegisterFile* registerFile = &exec->globalData().interpreter->registerFile();
m_globalVarStorageOffset = -RegisterFile::CallFrameHeaderSize - m_codeBlock->m_numParameters - registerFile->size();
m_globals.grow(symbolTable->size());
SymbolTable::iterator end = symbolTable->end();
for (SymbolTable::iterator it = symbolTable->begin(); it != end; ++it)
registerFor(it->second.getIndex()).setIndex(it->second.getIndex() + m_globalVarStorageOffset);
BatchedTransitionOptimizer optimizer(*m_globalData, globalObject);
const VarStack& varStack = programNode->varStack();
const FunctionStack& functionStack = programNode->functionStack();
bool canOptimizeNewGlobals = symbolTable->size() + functionStack.size() + varStack.size() < registerFile->maxGlobals();
if (canOptimizeNewGlobals) {
m_nextGlobalIndex -= symbolTable->size();
HashSet<StringImpl*, IdentifierRepHash> newGlobals;
Vector<std::pair<int, bool>, 16> functionInfo(functionStack.size());
for (size_t i = 0; i < functionStack.size(); ++i) {
FunctionBodyNode* function = functionStack[i];
globalObject->removeDirect(*m_globalData, function->ident()); SymbolTableEntry entry = symbolTable->inlineGet(function->ident().impl());
if (entry.isNull())
newGlobals.add(function->ident().impl());
functionInfo[i] = make_pair(entry.getIndex(), entry.isReadOnly());
}
Vector<bool, 16> shouldCreateVar(varStack.size());
for (size_t i = 0; i < varStack.size(); ++i) {
if (newGlobals.contains(varStack[i].first->impl()) || globalObject->hasProperty(exec, *varStack[i].first)) {
shouldCreateVar[i] = false;
continue;
}
shouldCreateVar[i] = true;
newGlobals.add(varStack[i].first->impl());
}
int expectedSize = symbolTable->size() + newGlobals.size();
globalObject->resizeRegisters(symbolTable->size(), expectedSize);
for (size_t i = 0; i < functionStack.size(); ++i) {
FunctionBodyNode* function = functionStack[i];
if (functionInfo[i].second)
continue;
RegisterID* dst = addGlobalVar(function->ident(), false);
JSValue value = new (exec) JSFunction(exec, makeFunction(exec, function), scopeChain);
globalObject->registerAt(dst->index() - m_globalVarStorageOffset).set(*m_globalData, globalObject, value);
}
for (size_t i = 0; i < varStack.size(); ++i) {
if (!shouldCreateVar[i])
continue;
addGlobalVar(*varStack[i].first, varStack[i].second & DeclarationStacks::IsConstant);
}
if (symbolTable->size() != expectedSize)
CRASH();
preserveLastVar();
} else {
for (size_t i = 0; i < functionStack.size(); ++i) {
FunctionBodyNode* function = functionStack[i];
globalObject->putWithAttributes(exec, function->ident(), new (exec) JSFunction(exec, makeFunction(exec, function), scopeChain), DontDelete);
}
for (size_t i = 0; i < varStack.size(); ++i) {
if (globalObject->symbolTableHasProperty(*varStack[i].first) || globalObject->hasProperty(exec, *varStack[i].first))
continue;
int attributes = DontDelete;
if (varStack[i].second & DeclarationStacks::IsConstant)
attributes |= ReadOnly;
globalObject->putWithAttributes(exec, *varStack[i].first, jsUndefined(), attributes);
}
preserveLastVar();
}
codeBlock->m_numCapturedVars = codeBlock->m_numVars;
}
BytecodeGenerator::BytecodeGenerator(FunctionBodyNode* functionBody, ScopeChainNode* scopeChain, SymbolTable* symbolTable, CodeBlock* codeBlock)
: m_shouldEmitDebugHooks(scopeChain->globalObject->debugger())
, m_shouldEmitProfileHooks(scopeChain->globalObject->supportsProfiling())
, m_shouldEmitRichSourceInfo(scopeChain->globalObject->supportsRichSourceInfo())
, m_scopeChain(*scopeChain->globalData, scopeChain)
, m_symbolTable(symbolTable)
, m_scopeNode(functionBody)
, m_codeBlock(codeBlock)
, m_activationRegister(0)
, m_finallyDepth(0)
, m_dynamicScopeDepth(0)
, m_baseScopeDepth(0)
, m_codeType(FunctionCode)
, m_nextConstantOffset(0)
, m_globalConstantIndex(0)
, m_hasCreatedActivation(false)
, m_firstLazyFunction(0)
, m_lastLazyFunction(0)
, m_globalData(scopeChain->globalData)
, m_lastOpcodeID(op_end)
#ifndef NDEBUG
, m_lastOpcodePosition(0)
#endif
, m_stack(m_globalData->stack())
, m_usesExceptions(false)
, m_expressionTooDeep(false)
{
if (m_shouldEmitDebugHooks)
m_codeBlock->setNeedsFullScopeChain(true);
codeBlock->setGlobalData(m_globalData);
emitOpcode(op_enter);
if (m_codeBlock->needsFullScopeChain()) {
m_activationRegister = addVar();
emitInitLazyRegister(m_activationRegister);
m_codeBlock->setActivationRegister(m_activationRegister->index());
}
if (m_codeBlock->needsFullScopeChain() || functionBody->usesArguments()) {
RegisterID* unmodifiedArgumentsRegister = addVar(); RegisterID* argumentsRegister = addVar(propertyNames().arguments, false);
codeBlock->setArgumentsRegister(argumentsRegister->index());
ASSERT_UNUSED(unmodifiedArgumentsRegister, unmodifiedArgumentsRegister->index() == JSC::unmodifiedArgumentsRegister(codeBlock->argumentsRegister()));
emitInitLazyRegister(argumentsRegister);
emitInitLazyRegister(unmodifiedArgumentsRegister);
if (m_codeBlock->isStrictMode()) {
emitOpcode(op_create_arguments);
instructions().append(argumentsRegister->index());
}
if (m_shouldEmitDebugHooks) {
emitOpcode(op_create_arguments);
instructions().append(argumentsRegister->index());
}
}
const DeclarationStacks::FunctionStack& functionStack = functionBody->functionStack();
const DeclarationStacks::VarStack& varStack = functionBody->varStack();
m_hasCreatedActivation = false;
if (functionBody->hasCapturedVariables()) {
for (size_t i = 0; i < functionStack.size(); ++i) {
FunctionBodyNode* function = functionStack[i];
const Identifier& ident = function->ident();
if (functionBody->captures(ident)) {
if (!m_hasCreatedActivation) {
m_hasCreatedActivation = true;
emitOpcode(op_create_activation);
instructions().append(m_activationRegister->index());
}
m_functions.add(ident.impl());
emitNewFunction(addVar(ident, false), function);
}
}
for (size_t i = 0; i < varStack.size(); ++i) {
const Identifier& ident = *varStack[i].first;
if (functionBody->captures(ident))
addVar(ident, varStack[i].second & DeclarationStacks::IsConstant);
}
}
bool canLazilyCreateFunctions = !functionBody->needsActivationForMoreThanVariables() && !m_shouldEmitDebugHooks;
if (!canLazilyCreateFunctions && !m_hasCreatedActivation) {
m_hasCreatedActivation = true;
emitOpcode(op_create_activation);
instructions().append(m_activationRegister->index());
}
codeBlock->m_numCapturedVars = codeBlock->m_numVars;
m_firstLazyFunction = codeBlock->m_numVars;
for (size_t i = 0; i < functionStack.size(); ++i) {
FunctionBodyNode* function = functionStack[i];
const Identifier& ident = function->ident();
if (!functionBody->captures(ident)) {
m_functions.add(ident.impl());
RefPtr<RegisterID> reg = addVar(ident, false);
if (!canLazilyCreateFunctions || ident == propertyNames().arguments)
emitNewFunction(reg.get(), function);
else {
emitInitLazyRegister(reg.get());
m_lazyFunctions.set(reg->index(), function);
}
}
}
m_lastLazyFunction = canLazilyCreateFunctions ? codeBlock->m_numVars : m_firstLazyFunction;
for (size_t i = 0; i < varStack.size(); ++i) {
const Identifier& ident = *varStack[i].first;
if (!functionBody->captures(ident))
addVar(ident, varStack[i].second & DeclarationStacks::IsConstant);
}
if (m_shouldEmitDebugHooks)
codeBlock->m_numCapturedVars = codeBlock->m_numVars;
FunctionParameters& parameters = *functionBody->parameters();
size_t parameterCount = parameters.size();
int nextParameterIndex = -RegisterFile::CallFrameHeaderSize - parameterCount - 1;
m_parameters.grow(1 + parameterCount);
m_thisRegister.setIndex(nextParameterIndex);
++m_codeBlock->m_numParameters;
for (size_t i = 0; i < parameterCount; ++i)
addParameter(parameters[i], ++nextParameterIndex);
preserveLastVar();
if (isConstructor()) {
RefPtr<RegisterID> func = newTemporary();
RefPtr<RegisterID> funcProto = newTemporary();
emitOpcode(op_get_callee);
instructions().append(func->index());
emitGetById(funcProto.get(), func.get(), globalData()->propertyNames->prototype);
emitOpcode(op_create_this);
instructions().append(m_thisRegister.index());
instructions().append(funcProto->index());
} else if (functionBody->usesThis() || m_shouldEmitDebugHooks) {
if (codeBlock->isStrictMode())
emitOpcode(op_convert_this_strict);
else
emitOpcode(op_convert_this);
instructions().append(m_thisRegister.index());
}
}
BytecodeGenerator::BytecodeGenerator(EvalNode* evalNode, ScopeChainNode* scopeChain, SymbolTable* symbolTable, EvalCodeBlock* codeBlock)
: m_shouldEmitDebugHooks(scopeChain->globalObject->debugger())
, m_shouldEmitProfileHooks(scopeChain->globalObject->supportsProfiling())
, m_shouldEmitRichSourceInfo(scopeChain->globalObject->supportsRichSourceInfo())
, m_scopeChain(*scopeChain->globalData, scopeChain)
, m_symbolTable(symbolTable)
, m_scopeNode(evalNode)
, m_codeBlock(codeBlock)
, m_thisRegister(RegisterFile::ProgramCodeThisRegister)
, m_finallyDepth(0)
, m_dynamicScopeDepth(0)
, m_baseScopeDepth(codeBlock->baseScopeDepth())
, m_codeType(EvalCode)
, m_nextConstantOffset(0)
, m_globalConstantIndex(0)
, m_hasCreatedActivation(true)
, m_firstLazyFunction(0)
, m_lastLazyFunction(0)
, m_globalData(scopeChain->globalData)
, m_lastOpcodeID(op_end)
#ifndef NDEBUG
, m_lastOpcodePosition(0)
#endif
, m_stack(m_globalData->stack())
, m_usesExceptions(false)
, m_expressionTooDeep(false)
{
if (m_shouldEmitDebugHooks || m_baseScopeDepth)
m_codeBlock->setNeedsFullScopeChain(true);
emitOpcode(op_enter);
codeBlock->setGlobalData(m_globalData);
m_codeBlock->m_numParameters = 1;
const DeclarationStacks::FunctionStack& functionStack = evalNode->functionStack();
for (size_t i = 0; i < functionStack.size(); ++i)
m_codeBlock->addFunctionDecl(makeFunction(m_globalData, functionStack[i]));
const DeclarationStacks::VarStack& varStack = evalNode->varStack();
unsigned numVariables = varStack.size();
Vector<Identifier> variables;
variables.reserveCapacity(numVariables);
for (size_t i = 0; i < numVariables; ++i)
variables.append(*varStack[i].first);
codeBlock->adoptVariables(variables);
codeBlock->m_numCapturedVars = codeBlock->m_numVars;
preserveLastVar();
}
RegisterID* BytecodeGenerator::emitInitLazyRegister(RegisterID* reg)
{
emitOpcode(op_init_lazy_reg);
instructions().append(reg->index());
return reg;
}
void BytecodeGenerator::addParameter(const Identifier& ident, int parameterIndex)
{
StringImpl* rep = ident.impl();
if (!m_functions.contains(rep)) {
symbolTable().set(rep, parameterIndex);
RegisterID& parameter = registerFor(parameterIndex);
parameter.setIndex(parameterIndex);
}
++m_codeBlock->m_numParameters;
}
RegisterID* BytecodeGenerator::registerFor(const Identifier& ident)
{
if (ident == propertyNames().thisIdentifier)
return &m_thisRegister;
if (!shouldOptimizeLocals())
return 0;
SymbolTableEntry entry = symbolTable().get(ident.impl());
if (entry.isNull())
return 0;
if (ident == propertyNames().arguments)
createArgumentsIfNecessary();
return createLazyRegisterIfNecessary(®isterFor(entry.getIndex()));
}
bool BytecodeGenerator::willResolveToArguments(const Identifier& ident)
{
if (ident != propertyNames().arguments)
return false;
if (!shouldOptimizeLocals())
return false;
SymbolTableEntry entry = symbolTable().get(ident.impl());
if (entry.isNull())
return false;
if (m_codeBlock->usesArguments() && m_codeType == FunctionCode)
return true;
return false;
}
RegisterID* BytecodeGenerator::uncheckedRegisterForArguments()
{
ASSERT(willResolveToArguments(propertyNames().arguments));
SymbolTableEntry entry = symbolTable().get(propertyNames().arguments.impl());
ASSERT(!entry.isNull());
return ®isterFor(entry.getIndex());
}
RegisterID* BytecodeGenerator::createLazyRegisterIfNecessary(RegisterID* reg)
{
if (m_lastLazyFunction <= reg->index() || reg->index() < m_firstLazyFunction)
return reg;
emitLazyNewFunction(reg, m_lazyFunctions.get(reg->index()));
return reg;
}
RegisterID* BytecodeGenerator::constRegisterFor(const Identifier& ident)
{
if (m_codeType == EvalCode)
return 0;
SymbolTableEntry entry = symbolTable().get(ident.impl());
if (entry.isNull())
return 0;
return createLazyRegisterIfNecessary(®isterFor(entry.getIndex()));
}
bool BytecodeGenerator::isLocal(const Identifier& ident)
{
if (ident == propertyNames().thisIdentifier)
return true;
return shouldOptimizeLocals() && symbolTable().contains(ident.impl());
}
bool BytecodeGenerator::isLocalConstant(const Identifier& ident)
{
return symbolTable().get(ident.impl()).isReadOnly();
}
RegisterID* BytecodeGenerator::newRegister()
{
m_calleeRegisters.append(m_calleeRegisters.size());
m_codeBlock->m_numCalleeRegisters = max<int>(m_codeBlock->m_numCalleeRegisters, m_calleeRegisters.size());
return &m_calleeRegisters.last();
}
RegisterID* BytecodeGenerator::newTemporary()
{
while (m_calleeRegisters.size() && !m_calleeRegisters.last().refCount())
m_calleeRegisters.removeLast();
RegisterID* result = newRegister();
result->setTemporary();
return result;
}
RegisterID* BytecodeGenerator::highestUsedRegister()
{
size_t count = m_codeBlock->m_numCalleeRegisters;
while (m_calleeRegisters.size() < count)
newRegister();
return &m_calleeRegisters.last();
}
PassRefPtr<LabelScope> BytecodeGenerator::newLabelScope(LabelScope::Type type, const Identifier* name)
{
while (m_labelScopes.size() && !m_labelScopes.last().refCount())
m_labelScopes.removeLast();
LabelScope scope(type, name, scopeDepth(), newLabel(), type == LabelScope::Loop ? newLabel() : PassRefPtr<Label>()); m_labelScopes.append(scope);
return &m_labelScopes.last();
}
PassRefPtr<Label> BytecodeGenerator::newLabel()
{
while (m_labels.size() && !m_labels.last().refCount())
m_labels.removeLast();
m_labels.append(m_codeBlock);
return &m_labels.last();
}
PassRefPtr<Label> BytecodeGenerator::emitLabel(Label* l0)
{
unsigned newLabelIndex = instructions().size();
l0->setLocation(newLabelIndex);
if (m_codeBlock->numberOfJumpTargets()) {
unsigned lastLabelIndex = m_codeBlock->lastJumpTarget();
ASSERT(lastLabelIndex <= newLabelIndex);
if (newLabelIndex == lastLabelIndex) {
return l0;
}
}
m_codeBlock->addJumpTarget(newLabelIndex);
m_lastOpcodeID = op_end;
return l0;
}
void BytecodeGenerator::emitOpcode(OpcodeID opcodeID)
{
#ifndef NDEBUG
size_t opcodePosition = instructions().size();
ASSERT(opcodePosition - m_lastOpcodePosition == opcodeLength(m_lastOpcodeID) || m_lastOpcodeID == op_end);
m_lastOpcodePosition = opcodePosition;
#endif
instructions().append(globalData()->interpreter->getOpcode(opcodeID));
m_lastOpcodeID = opcodeID;
}
void BytecodeGenerator::retrieveLastBinaryOp(int& dstIndex, int& src1Index, int& src2Index)
{
ASSERT(instructions().size() >= 4);
size_t size = instructions().size();
dstIndex = instructions().at(size - 3).u.operand;
src1Index = instructions().at(size - 2).u.operand;
src2Index = instructions().at(size - 1).u.operand;
}
void BytecodeGenerator::retrieveLastUnaryOp(int& dstIndex, int& srcIndex)
{
ASSERT(instructions().size() >= 3);
size_t size = instructions().size();
dstIndex = instructions().at(size - 2).u.operand;
srcIndex = instructions().at(size - 1).u.operand;
}
void ALWAYS_INLINE BytecodeGenerator::rewindBinaryOp()
{
ASSERT(instructions().size() >= 4);
instructions().shrink(instructions().size() - 4);
m_lastOpcodeID = op_end;
}
void ALWAYS_INLINE BytecodeGenerator::rewindUnaryOp()
{
ASSERT(instructions().size() >= 3);
instructions().shrink(instructions().size() - 3);
m_lastOpcodeID = op_end;
}
PassRefPtr<Label> BytecodeGenerator::emitJump(Label* target)
{
size_t begin = instructions().size();
emitOpcode(target->isForward() ? op_jmp : op_loop);
instructions().append(target->bind(begin, instructions().size()));
return target;
}
PassRefPtr<Label> BytecodeGenerator::emitJumpIfTrue(RegisterID* cond, Label* target)
{
if (m_lastOpcodeID == op_less) {
int dstIndex;
int src1Index;
int src2Index;
retrieveLastBinaryOp(dstIndex, src1Index, src2Index);
if (cond->index() == dstIndex && cond->isTemporary() && !cond->refCount()) {
rewindBinaryOp();
size_t begin = instructions().size();
emitOpcode(target->isForward() ? op_jless : op_loop_if_less);
instructions().append(src1Index);
instructions().append(src2Index);
instructions().append(target->bind(begin, instructions().size()));
return target;
}
} else if (m_lastOpcodeID == op_lesseq) {
int dstIndex;
int src1Index;
int src2Index;
retrieveLastBinaryOp(dstIndex, src1Index, src2Index);
if (cond->index() == dstIndex && cond->isTemporary() && !cond->refCount()) {
rewindBinaryOp();
size_t begin = instructions().size();
emitOpcode(target->isForward() ? op_jlesseq : op_loop_if_lesseq);
instructions().append(src1Index);
instructions().append(src2Index);
instructions().append(target->bind(begin, instructions().size()));
return target;
}
} else if (m_lastOpcodeID == op_eq_null && target->isForward()) {
int dstIndex;
int srcIndex;
retrieveLastUnaryOp(dstIndex, srcIndex);
if (cond->index() == dstIndex && cond->isTemporary() && !cond->refCount()) {
rewindUnaryOp();
size_t begin = instructions().size();
emitOpcode(op_jeq_null);
instructions().append(srcIndex);
instructions().append(target->bind(begin, instructions().size()));
return target;
}
} else if (m_lastOpcodeID == op_neq_null && target->isForward()) {
int dstIndex;
int srcIndex;
retrieveLastUnaryOp(dstIndex, srcIndex);
if (cond->index() == dstIndex && cond->isTemporary() && !cond->refCount()) {
rewindUnaryOp();
size_t begin = instructions().size();
emitOpcode(op_jneq_null);
instructions().append(srcIndex);
instructions().append(target->bind(begin, instructions().size()));
return target;
}
}
size_t begin = instructions().size();
emitOpcode(target->isForward() ? op_jtrue : op_loop_if_true);
instructions().append(cond->index());
instructions().append(target->bind(begin, instructions().size()));
return target;
}
PassRefPtr<Label> BytecodeGenerator::emitJumpIfFalse(RegisterID* cond, Label* target)
{
if (m_lastOpcodeID == op_less && target->isForward()) {
int dstIndex;
int src1Index;
int src2Index;
retrieveLastBinaryOp(dstIndex, src1Index, src2Index);
if (cond->index() == dstIndex && cond->isTemporary() && !cond->refCount()) {
rewindBinaryOp();
size_t begin = instructions().size();
emitOpcode(op_jnless);
instructions().append(src1Index);
instructions().append(src2Index);
instructions().append(target->bind(begin, instructions().size()));
return target;
}
} else if (m_lastOpcodeID == op_lesseq && target->isForward()) {
int dstIndex;
int src1Index;
int src2Index;
retrieveLastBinaryOp(dstIndex, src1Index, src2Index);
if (cond->index() == dstIndex && cond->isTemporary() && !cond->refCount()) {
rewindBinaryOp();
size_t begin = instructions().size();
emitOpcode(op_jnlesseq);
instructions().append(src1Index);
instructions().append(src2Index);
instructions().append(target->bind(begin, instructions().size()));
return target;
}
} else if (m_lastOpcodeID == op_not) {
int dstIndex;
int srcIndex;
retrieveLastUnaryOp(dstIndex, srcIndex);
if (cond->index() == dstIndex && cond->isTemporary() && !cond->refCount()) {
rewindUnaryOp();
size_t begin = instructions().size();
emitOpcode(target->isForward() ? op_jtrue : op_loop_if_true);
instructions().append(srcIndex);
instructions().append(target->bind(begin, instructions().size()));
return target;
}
} else if (m_lastOpcodeID == op_eq_null && target->isForward()) {
int dstIndex;
int srcIndex;
retrieveLastUnaryOp(dstIndex, srcIndex);
if (cond->index() == dstIndex && cond->isTemporary() && !cond->refCount()) {
rewindUnaryOp();
size_t begin = instructions().size();
emitOpcode(op_jneq_null);
instructions().append(srcIndex);
instructions().append(target->bind(begin, instructions().size()));
return target;
}
} else if (m_lastOpcodeID == op_neq_null && target->isForward()) {
int dstIndex;
int srcIndex;
retrieveLastUnaryOp(dstIndex, srcIndex);
if (cond->index() == dstIndex && cond->isTemporary() && !cond->refCount()) {
rewindUnaryOp();
size_t begin = instructions().size();
emitOpcode(op_jeq_null);
instructions().append(srcIndex);
instructions().append(target->bind(begin, instructions().size()));
return target;
}
}
size_t begin = instructions().size();
emitOpcode(target->isForward() ? op_jfalse : op_loop_if_false);
instructions().append(cond->index());
instructions().append(target->bind(begin, instructions().size()));
return target;
}
PassRefPtr<Label> BytecodeGenerator::emitJumpIfNotFunctionCall(RegisterID* cond, Label* target)
{
size_t begin = instructions().size();
emitOpcode(op_jneq_ptr);
instructions().append(cond->index());
instructions().append(Instruction(*m_globalData, m_codeBlock->ownerExecutable(), m_scopeChain->globalObject->callFunction()));
instructions().append(target->bind(begin, instructions().size()));
return target;
}
PassRefPtr<Label> BytecodeGenerator::emitJumpIfNotFunctionApply(RegisterID* cond, Label* target)
{
size_t begin = instructions().size();
emitOpcode(op_jneq_ptr);
instructions().append(cond->index());
instructions().append(Instruction(*m_globalData, m_codeBlock->ownerExecutable(), m_scopeChain->globalObject->applyFunction()));
instructions().append(target->bind(begin, instructions().size()));
return target;
}
unsigned BytecodeGenerator::addConstant(const Identifier& ident)
{
StringImpl* rep = ident.impl();
pair<IdentifierMap::iterator, bool> result = m_identifierMap.add(rep, m_codeBlock->numberOfIdentifiers());
if (result.second) m_codeBlock->addIdentifier(Identifier(m_globalData, rep));
return result.first->second;
}
RegisterID* BytecodeGenerator::addConstantValue(JSValue v)
{
int index = m_nextConstantOffset;
pair<JSValueMap::iterator, bool> result = m_jsValueMap.add(JSValue::encode(v), m_nextConstantOffset);
if (result.second) {
m_constantPoolRegisters.append(FirstConstantRegisterIndex + m_nextConstantOffset);
++m_nextConstantOffset;
m_codeBlock->addConstant(JSValue(v));
} else
index = result.first->second;
return &m_constantPoolRegisters[index];
}
unsigned BytecodeGenerator::addRegExp(RegExp* r)
{
return m_codeBlock->addRegExp(r);
}
RegisterID* BytecodeGenerator::emitMove(RegisterID* dst, RegisterID* src)
{
emitOpcode(op_mov);
instructions().append(dst->index());
instructions().append(src->index());
return dst;
}
RegisterID* BytecodeGenerator::emitUnaryOp(OpcodeID opcodeID, RegisterID* dst, RegisterID* src)
{
emitOpcode(opcodeID);
instructions().append(dst->index());
instructions().append(src->index());
return dst;
}
RegisterID* BytecodeGenerator::emitPreInc(RegisterID* srcDst)
{
emitOpcode(op_pre_inc);
instructions().append(srcDst->index());
return srcDst;
}
RegisterID* BytecodeGenerator::emitPreDec(RegisterID* srcDst)
{
emitOpcode(op_pre_dec);
instructions().append(srcDst->index());
return srcDst;
}
RegisterID* BytecodeGenerator::emitPostInc(RegisterID* dst, RegisterID* srcDst)
{
emitOpcode(op_post_inc);
instructions().append(dst->index());
instructions().append(srcDst->index());
return dst;
}
RegisterID* BytecodeGenerator::emitPostDec(RegisterID* dst, RegisterID* srcDst)
{
emitOpcode(op_post_dec);
instructions().append(dst->index());
instructions().append(srcDst->index());
return dst;
}
RegisterID* BytecodeGenerator::emitBinaryOp(OpcodeID opcodeID, RegisterID* dst, RegisterID* src1, RegisterID* src2, OperandTypes types)
{
emitOpcode(opcodeID);
instructions().append(dst->index());
instructions().append(src1->index());
instructions().append(src2->index());
if (opcodeID == op_bitor || opcodeID == op_bitand || opcodeID == op_bitxor ||
opcodeID == op_add || opcodeID == op_mul || opcodeID == op_sub || opcodeID == op_div)
instructions().append(types.toInt());
return dst;
}
RegisterID* BytecodeGenerator::emitEqualityOp(OpcodeID opcodeID, RegisterID* dst, RegisterID* src1, RegisterID* src2)
{
if (m_lastOpcodeID == op_typeof) {
int dstIndex;
int srcIndex;
retrieveLastUnaryOp(dstIndex, srcIndex);
if (src1->index() == dstIndex
&& src1->isTemporary()
&& m_codeBlock->isConstantRegisterIndex(src2->index())
&& m_codeBlock->constantRegister(src2->index()).get().isString()) {
const UString& value = asString(m_codeBlock->constantRegister(src2->index()).get())->tryGetValue();
if (value == "undefined") {
rewindUnaryOp();
emitOpcode(op_is_undefined);
instructions().append(dst->index());
instructions().append(srcIndex);
return dst;
}
if (value == "boolean") {
rewindUnaryOp();
emitOpcode(op_is_boolean);
instructions().append(dst->index());
instructions().append(srcIndex);
return dst;
}
if (value == "number") {
rewindUnaryOp();
emitOpcode(op_is_number);
instructions().append(dst->index());
instructions().append(srcIndex);
return dst;
}
if (value == "string") {
rewindUnaryOp();
emitOpcode(op_is_string);
instructions().append(dst->index());
instructions().append(srcIndex);
return dst;
}
if (value == "object") {
rewindUnaryOp();
emitOpcode(op_is_object);
instructions().append(dst->index());
instructions().append(srcIndex);
return dst;
}
if (value == "function") {
rewindUnaryOp();
emitOpcode(op_is_function);
instructions().append(dst->index());
instructions().append(srcIndex);
return dst;
}
}
}
emitOpcode(opcodeID);
instructions().append(dst->index());
instructions().append(src1->index());
instructions().append(src2->index());
return dst;
}
RegisterID* BytecodeGenerator::emitLoad(RegisterID* dst, bool b)
{
return emitLoad(dst, jsBoolean(b));
}
RegisterID* BytecodeGenerator::emitLoad(RegisterID* dst, double number)
{
if (isnan(number) || number == HashTraits<double>::emptyValue() || HashTraits<double>::isDeletedValue(number))
return emitLoad(dst, jsNumber(number));
JSValue& valueInMap = m_numberMap.add(number, JSValue()).first->second;
if (!valueInMap)
valueInMap = jsNumber(number);
return emitLoad(dst, valueInMap);
}
RegisterID* BytecodeGenerator::emitLoad(RegisterID* dst, const Identifier& identifier)
{
JSString*& stringInMap = m_stringMap.add(identifier.impl(), 0).first->second;
if (!stringInMap)
stringInMap = jsOwnedString(globalData(), identifier.ustring());
return emitLoad(dst, JSValue(stringInMap));
}
RegisterID* BytecodeGenerator::emitLoad(RegisterID* dst, JSValue v)
{
RegisterID* constantID = addConstantValue(v);
if (dst)
return emitMove(dst, constantID);
return constantID;
}
bool BytecodeGenerator::findScopedProperty(const Identifier& property, int& index, size_t& stackDepth, bool forWriting, bool& requiresDynamicChecks, JSObject*& globalObject)
{
if (property == propertyNames().arguments || !canOptimizeNonLocals()) {
stackDepth = 0;
index = missingSymbolMarker();
if (shouldOptimizeLocals() && m_codeType == GlobalCode) {
ScopeChainIterator iter = m_scopeChain->begin();
globalObject = iter->get();
ASSERT((++iter) == m_scopeChain->end());
}
return false;
}
size_t depth = 0;
requiresDynamicChecks = false;
ScopeChainIterator iter = m_scopeChain->begin();
ScopeChainIterator end = m_scopeChain->end();
for (; iter != end; ++iter, ++depth) {
JSObject* currentScope = iter->get();
if (!currentScope->isVariableObject())
break;
JSVariableObject* currentVariableObject = static_cast<JSVariableObject*>(currentScope);
SymbolTableEntry entry = currentVariableObject->symbolTable().get(property.impl());
if (!entry.isNull()) {
if (entry.isReadOnly() && forWriting) {
stackDepth = 0;
index = missingSymbolMarker();
if (++iter == end)
globalObject = currentVariableObject;
return false;
}
stackDepth = depth + m_codeBlock->needsFullScopeChain();
index = entry.getIndex();
if (++iter == end)
globalObject = currentVariableObject;
return true;
}
bool scopeRequiresDynamicChecks = false;
if (currentVariableObject->isDynamicScope(scopeRequiresDynamicChecks))
break;
requiresDynamicChecks |= scopeRequiresDynamicChecks;
}
stackDepth = depth + m_codeBlock->needsFullScopeChain();
index = missingSymbolMarker();
JSObject* scope = iter->get();
if (++iter == end)
globalObject = scope;
return true;
}
void BytecodeGenerator::emitCheckHasInstance(RegisterID* base)
{
emitOpcode(op_check_has_instance);
instructions().append(base->index());
}
RegisterID* BytecodeGenerator::emitInstanceOf(RegisterID* dst, RegisterID* value, RegisterID* base, RegisterID* basePrototype)
{
emitOpcode(op_instanceof);
instructions().append(dst->index());
instructions().append(value->index());
instructions().append(base->index());
instructions().append(basePrototype->index());
return dst;
}
static const unsigned maxGlobalResolves = 128;
bool BytecodeGenerator::shouldAvoidResolveGlobal()
{
return m_codeBlock->globalResolveInfoCount() > maxGlobalResolves && !m_labelScopes.size();
}
RegisterID* BytecodeGenerator::emitResolve(RegisterID* dst, const Identifier& property)
{
size_t depth = 0;
int index = 0;
JSObject* globalObject = 0;
bool requiresDynamicChecks = false;
if (!findScopedProperty(property, index, depth, false, requiresDynamicChecks, globalObject) && !globalObject) {
emitOpcode(op_resolve);
instructions().append(dst->index());
instructions().append(addConstant(property));
return dst;
}
if (shouldAvoidResolveGlobal()) {
globalObject = 0;
requiresDynamicChecks = true;
}
if (globalObject) {
bool forceGlobalResolve = false;
if (index != missingSymbolMarker() && !forceGlobalResolve && !requiresDynamicChecks) {
return emitGetScopedVar(dst, depth, index, globalObject);
}
#if ENABLE(JIT)
m_codeBlock->addGlobalResolveInfo(instructions().size());
#endif
#if ENABLE(INTERPRETER)
m_codeBlock->addGlobalResolveInstruction(instructions().size());
#endif
emitOpcode(requiresDynamicChecks ? op_resolve_global_dynamic : op_resolve_global);
instructions().append(dst->index());
instructions().append(addConstant(property));
instructions().append(0);
instructions().append(0);
if (requiresDynamicChecks)
instructions().append(depth);
return dst;
}
if (requiresDynamicChecks) {
emitOpcode(op_resolve);
instructions().append(dst->index());
instructions().append(addConstant(property));
return dst;
}
if (index != missingSymbolMarker()) {
return emitGetScopedVar(dst, depth, index, globalObject);
}
emitOpcode(op_resolve_skip);
instructions().append(dst->index());
instructions().append(addConstant(property));
instructions().append(depth);
return dst;
}
RegisterID* BytecodeGenerator::emitGetScopedVar(RegisterID* dst, size_t depth, int index, JSValue globalObject)
{
if (globalObject) {
emitOpcode(op_get_global_var);
instructions().append(dst->index());
instructions().append(index);
return dst;
}
emitOpcode(op_get_scoped_var);
instructions().append(dst->index());
instructions().append(index);
instructions().append(depth);
return dst;
}
RegisterID* BytecodeGenerator::emitPutScopedVar(size_t depth, int index, RegisterID* value, JSValue globalObject)
{
if (globalObject) {
emitOpcode(op_put_global_var);
instructions().append(index);
instructions().append(value->index());
return value;
}
emitOpcode(op_put_scoped_var);
instructions().append(index);
instructions().append(depth);
instructions().append(value->index());
return value;
}
RegisterID* BytecodeGenerator::emitResolveBase(RegisterID* dst, const Identifier& property)
{
size_t depth = 0;
int index = 0;
JSObject* globalObject = 0;
bool requiresDynamicChecks = false;
findScopedProperty(property, index, depth, false, requiresDynamicChecks, globalObject);
if (!globalObject || requiresDynamicChecks) {
emitOpcode(op_resolve_base);
instructions().append(dst->index());
instructions().append(addConstant(property));
instructions().append(false);
return dst;
}
return emitLoad(dst, JSValue(globalObject));
}
RegisterID* BytecodeGenerator::emitResolveBaseForPut(RegisterID* dst, const Identifier& property)
{
if (!m_codeBlock->isStrictMode())
return emitResolveBase(dst, property);
size_t depth = 0;
int index = 0;
JSObject* globalObject = 0;
bool requiresDynamicChecks = false;
findScopedProperty(property, index, depth, false, requiresDynamicChecks, globalObject);
if (!globalObject || requiresDynamicChecks) {
emitOpcode(op_resolve_base);
instructions().append(dst->index());
instructions().append(addConstant(property));
instructions().append(true);
return dst;
}
RefPtr<RegisterID> result = emitLoad(dst, JSValue(globalObject));
emitOpcode(op_ensure_property_exists);
instructions().append(dst->index());
instructions().append(addConstant(property));
return result.get();
}
RegisterID* BytecodeGenerator::emitResolveWithBase(RegisterID* baseDst, RegisterID* propDst, const Identifier& property)
{
size_t depth = 0;
int index = 0;
JSObject* globalObject = 0;
bool requiresDynamicChecks = false;
if (!findScopedProperty(property, index, depth, false, requiresDynamicChecks, globalObject) || !globalObject || requiresDynamicChecks) {
emitOpcode(op_resolve_with_base);
instructions().append(baseDst->index());
instructions().append(propDst->index());
instructions().append(addConstant(property));
return baseDst;
}
bool forceGlobalResolve = false;
emitLoad(baseDst, JSValue(globalObject));
if (index != missingSymbolMarker() && !forceGlobalResolve) {
emitGetScopedVar(propDst, depth, index, globalObject);
return baseDst;
}
if (shouldAvoidResolveGlobal()) {
emitOpcode(op_resolve);
instructions().append(propDst->index());
instructions().append(addConstant(property));
return baseDst;
}
#if ENABLE(JIT)
m_codeBlock->addGlobalResolveInfo(instructions().size());
#endif
#if ENABLE(INTERPRETER)
m_codeBlock->addGlobalResolveInstruction(instructions().size());
#endif
emitOpcode(requiresDynamicChecks ? op_resolve_global_dynamic : op_resolve_global);
instructions().append(propDst->index());
instructions().append(addConstant(property));
instructions().append(0);
instructions().append(0);
if (requiresDynamicChecks)
instructions().append(depth);
return baseDst;
}
void BytecodeGenerator::emitMethodCheck()
{
emitOpcode(op_method_check);
}
RegisterID* BytecodeGenerator::emitGetById(RegisterID* dst, RegisterID* base, const Identifier& property)
{
#if ENABLE(JIT)
m_codeBlock->addStructureStubInfo(StructureStubInfo(access_get_by_id));
#endif
#if ENABLE(INTERPRETER)
m_codeBlock->addPropertyAccessInstruction(instructions().size());
#endif
emitOpcode(op_get_by_id);
instructions().append(dst->index());
instructions().append(base->index());
instructions().append(addConstant(property));
instructions().append(0);
instructions().append(0);
instructions().append(0);
instructions().append(0);
return dst;
}
RegisterID* BytecodeGenerator::emitGetArgumentsLength(RegisterID* dst, RegisterID* base)
{
emitOpcode(op_get_arguments_length);
instructions().append(dst->index());
ASSERT(base->index() == m_codeBlock->argumentsRegister());
instructions().append(base->index());
instructions().append(addConstant(propertyNames().length));
return dst;
}
RegisterID* BytecodeGenerator::emitPutById(RegisterID* base, const Identifier& property, RegisterID* value)
{
#if ENABLE(JIT)
m_codeBlock->addStructureStubInfo(StructureStubInfo(access_put_by_id));
#endif
#if ENABLE(INTERPRETER)
m_codeBlock->addPropertyAccessInstruction(instructions().size());
#endif
emitOpcode(op_put_by_id);
instructions().append(base->index());
instructions().append(addConstant(property));
instructions().append(value->index());
instructions().append(0);
instructions().append(0);
instructions().append(0);
instructions().append(0);
instructions().append(0);
return value;
}
RegisterID* BytecodeGenerator::emitDirectPutById(RegisterID* base, const Identifier& property, RegisterID* value)
{
#if ENABLE(JIT)
m_codeBlock->addStructureStubInfo(StructureStubInfo(access_put_by_id));
#endif
#if ENABLE(INTERPRETER)
m_codeBlock->addPropertyAccessInstruction(instructions().size());
#endif
emitOpcode(op_put_by_id);
instructions().append(base->index());
instructions().append(addConstant(property));
instructions().append(value->index());
instructions().append(0);
instructions().append(0);
instructions().append(0);
instructions().append(0);
instructions().append(property != m_globalData->propertyNames->underscoreProto);
return value;
}
RegisterID* BytecodeGenerator::emitPutGetter(RegisterID* base, const Identifier& property, RegisterID* value)
{
emitOpcode(op_put_getter);
instructions().append(base->index());
instructions().append(addConstant(property));
instructions().append(value->index());
return value;
}
RegisterID* BytecodeGenerator::emitPutSetter(RegisterID* base, const Identifier& property, RegisterID* value)
{
emitOpcode(op_put_setter);
instructions().append(base->index());
instructions().append(addConstant(property));
instructions().append(value->index());
return value;
}
RegisterID* BytecodeGenerator::emitDeleteById(RegisterID* dst, RegisterID* base, const Identifier& property)
{
emitOpcode(op_del_by_id);
instructions().append(dst->index());
instructions().append(base->index());
instructions().append(addConstant(property));
return dst;
}
RegisterID* BytecodeGenerator::emitGetArgumentByVal(RegisterID* dst, RegisterID* base, RegisterID* property)
{
emitOpcode(op_get_argument_by_val);
instructions().append(dst->index());
ASSERT(base->index() == m_codeBlock->argumentsRegister());
instructions().append(base->index());
instructions().append(property->index());
return dst;
}
RegisterID* BytecodeGenerator::emitGetByVal(RegisterID* dst, RegisterID* base, RegisterID* property)
{
for (size_t i = m_forInContextStack.size(); i > 0; i--) {
ForInContext& context = m_forInContextStack[i - 1];
if (context.propertyRegister == property) {
emitOpcode(op_get_by_pname);
instructions().append(dst->index());
instructions().append(base->index());
instructions().append(property->index());
instructions().append(context.expectedSubscriptRegister->index());
instructions().append(context.iterRegister->index());
instructions().append(context.indexRegister->index());
return dst;
}
}
emitOpcode(op_get_by_val);
instructions().append(dst->index());
instructions().append(base->index());
instructions().append(property->index());
return dst;
}
RegisterID* BytecodeGenerator::emitPutByVal(RegisterID* base, RegisterID* property, RegisterID* value)
{
emitOpcode(op_put_by_val);
instructions().append(base->index());
instructions().append(property->index());
instructions().append(value->index());
return value;
}
RegisterID* BytecodeGenerator::emitDeleteByVal(RegisterID* dst, RegisterID* base, RegisterID* property)
{
emitOpcode(op_del_by_val);
instructions().append(dst->index());
instructions().append(base->index());
instructions().append(property->index());
return dst;
}
RegisterID* BytecodeGenerator::emitPutByIndex(RegisterID* base, unsigned index, RegisterID* value)
{
emitOpcode(op_put_by_index);
instructions().append(base->index());
instructions().append(index);
instructions().append(value->index());
return value;
}
RegisterID* BytecodeGenerator::emitNewObject(RegisterID* dst)
{
emitOpcode(op_new_object);
instructions().append(dst->index());
return dst;
}
unsigned BytecodeGenerator::addConstantBuffer(unsigned length)
{
return m_codeBlock->addConstantBuffer(length);
}
JSString* BytecodeGenerator::addStringConstant(const Identifier& identifier)
{
JSString*& stringInMap = m_stringMap.add(identifier.impl(), 0).first->second;
if (!stringInMap) {
stringInMap = jsString(globalData(), identifier.ustring());
addConstantValue(stringInMap);
}
return stringInMap;
}
RegisterID* BytecodeGenerator::emitNewArray(RegisterID* dst, ElementNode* elements, unsigned length)
{
#if !ASSERT_DISABLED
unsigned checkLength = 0;
#endif
bool hadVariableExpression = false;
if (length) {
for (ElementNode* n = elements; n; n = n->next()) {
if (!n->value()->isNumber() && !n->value()->isString()) {
hadVariableExpression = true;
break;
}
if (n->elision())
break;
#if !ASSERT_DISABLED
checkLength++;
#endif
}
if (!hadVariableExpression) {
ASSERT(length == checkLength);
unsigned constantBufferIndex = addConstantBuffer(length);
JSValue* constantBuffer = m_codeBlock->constantBuffer(constantBufferIndex);
unsigned index = 0;
for (ElementNode* n = elements; index < length; n = n->next()) {
if (n->value()->isNumber())
constantBuffer[index++] = jsNumber(static_cast<NumberNode*>(n->value())->value());
else {
ASSERT(n->value()->isString());
constantBuffer[index++] = addStringConstant(static_cast<StringNode*>(n->value())->value());
}
}
emitOpcode(op_new_array_buffer);
instructions().append(dst->index());
instructions().append(constantBufferIndex);
instructions().append(length);
return dst;
}
}
Vector<RefPtr<RegisterID>, 16> argv;
for (ElementNode* n = elements; n; n = n->next()) {
if (n->elision())
break;
argv.append(newTemporary());
ASSERT(argv.size() == 1 || argv[argv.size() - 1]->index() == argv[argv.size() - 2]->index() + 1);
emitNode(argv.last().get(), n->value());
}
emitOpcode(op_new_array);
instructions().append(dst->index());
instructions().append(argv.size() ? argv[0]->index() : 0); instructions().append(argv.size()); return dst;
}
RegisterID* BytecodeGenerator::emitNewFunction(RegisterID* dst, FunctionBodyNode* function)
{
return emitNewFunctionInternal(dst, m_codeBlock->addFunctionDecl(makeFunction(m_globalData, function)), false);
}
RegisterID* BytecodeGenerator::emitLazyNewFunction(RegisterID* dst, FunctionBodyNode* function)
{
std::pair<FunctionOffsetMap::iterator, bool> ptr = m_functionOffsets.add(function, 0);
if (ptr.second)
ptr.first->second = m_codeBlock->addFunctionDecl(makeFunction(m_globalData, function));
return emitNewFunctionInternal(dst, ptr.first->second, true);
}
RegisterID* BytecodeGenerator::emitNewFunctionInternal(RegisterID* dst, unsigned index, bool doNullCheck)
{
createActivationIfNecessary();
emitOpcode(op_new_func);
instructions().append(dst->index());
instructions().append(index);
instructions().append(doNullCheck);
return dst;
}
RegisterID* BytecodeGenerator::emitNewRegExp(RegisterID* dst, RegExp* regExp)
{
emitOpcode(op_new_regexp);
instructions().append(dst->index());
instructions().append(addRegExp(regExp));
return dst;
}
RegisterID* BytecodeGenerator::emitNewFunctionExpression(RegisterID* r0, FuncExprNode* n)
{
FunctionBodyNode* function = n->body();
unsigned index = m_codeBlock->addFunctionExpr(makeFunction(m_globalData, function));
createActivationIfNecessary();
emitOpcode(op_new_func_exp);
instructions().append(r0->index());
instructions().append(index);
return r0;
}
RegisterID* BytecodeGenerator::emitCall(RegisterID* dst, RegisterID* func, CallArguments& callArguments, unsigned divot, unsigned startOffset, unsigned endOffset)
{
return emitCall(op_call, dst, func, callArguments, divot, startOffset, endOffset);
}
void BytecodeGenerator::createArgumentsIfNecessary()
{
if (m_codeType != FunctionCode)
return;
if (!m_codeBlock->usesArguments())
return;
if (m_codeBlock->isStrictMode())
return;
emitOpcode(op_create_arguments);
instructions().append(m_codeBlock->argumentsRegister());
}
void BytecodeGenerator::createActivationIfNecessary()
{
if (m_hasCreatedActivation)
return;
if (!m_codeBlock->needsFullScopeChain())
return;
emitOpcode(op_create_activation);
instructions().append(m_activationRegister->index());
}
RegisterID* BytecodeGenerator::emitCallEval(RegisterID* dst, RegisterID* func, CallArguments& callArguments, unsigned divot, unsigned startOffset, unsigned endOffset)
{
return emitCall(op_call_eval, dst, func, callArguments, divot, startOffset, endOffset);
}
RegisterID* BytecodeGenerator::emitCall(OpcodeID opcodeID, RegisterID* dst, RegisterID* func, CallArguments& callArguments, unsigned divot, unsigned startOffset, unsigned endOffset)
{
ASSERT(opcodeID == op_call || opcodeID == op_call_eval);
ASSERT(func->refCount());
if (m_shouldEmitProfileHooks)
emitMove(callArguments.profileHookRegister(), func);
unsigned argumentIndex = 0;
for (ArgumentListNode* n = callArguments.argumentsNode()->m_listNode; n; n = n->m_next)
emitNode(callArguments.argumentRegister(argumentIndex++), n);
Vector<RefPtr<RegisterID>, RegisterFile::CallFrameHeaderSize> callFrame;
for (int i = 0; i < RegisterFile::CallFrameHeaderSize; ++i)
callFrame.append(newTemporary());
if (m_shouldEmitProfileHooks) {
emitOpcode(op_profile_will_call);
instructions().append(callArguments.profileHookRegister()->index());
}
emitExpressionInfo(divot, startOffset, endOffset);
#if ENABLE(JIT)
m_codeBlock->addCallLinkInfo();
#endif
emitOpcode(opcodeID);
instructions().append(func->index()); instructions().append(callArguments.count()); instructions().append(callArguments.callFrame()); if (dst != ignoredResult()) {
emitOpcode(op_call_put_result);
instructions().append(dst->index()); }
if (m_shouldEmitProfileHooks) {
emitOpcode(op_profile_did_call);
instructions().append(callArguments.profileHookRegister()->index());
}
return dst;
}
RegisterID* BytecodeGenerator::emitLoadVarargs(RegisterID* argCountDst, RegisterID* thisRegister, RegisterID* arguments)
{
ASSERT(argCountDst->index() < arguments->index());
emitOpcode(op_load_varargs);
instructions().append(argCountDst->index());
instructions().append(arguments->index());
instructions().append(thisRegister->index() + RegisterFile::CallFrameHeaderSize); return argCountDst;
}
RegisterID* BytecodeGenerator::emitCallVarargs(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* argCountRegister, unsigned divot, unsigned startOffset, unsigned endOffset)
{
ASSERT(func->refCount());
ASSERT(thisRegister->refCount());
ASSERT(dst != func);
if (m_shouldEmitProfileHooks) {
emitOpcode(op_profile_will_call);
instructions().append(func->index());
}
emitExpressionInfo(divot, startOffset, endOffset);
emitOpcode(op_call_varargs);
instructions().append(func->index()); instructions().append(argCountRegister->index()); instructions().append(thisRegister->index() + RegisterFile::CallFrameHeaderSize); if (dst != ignoredResult()) {
emitOpcode(op_call_put_result);
instructions().append(dst->index()); }
if (m_shouldEmitProfileHooks) {
emitOpcode(op_profile_did_call);
instructions().append(func->index());
}
return dst;
}
RegisterID* BytecodeGenerator::emitReturn(RegisterID* src)
{
if (m_codeBlock->needsFullScopeChain()) {
emitOpcode(op_tear_off_activation);
instructions().append(m_activationRegister->index());
instructions().append(m_codeBlock->argumentsRegister());
} else if (m_codeBlock->usesArguments() && m_codeBlock->m_numParameters > 1
&& !m_codeBlock->isStrictMode()) { emitOpcode(op_tear_off_arguments);
instructions().append(m_codeBlock->argumentsRegister());
}
if (isConstructor() && (src->index() != m_thisRegister.index())) {
emitOpcode(op_ret_object_or_this);
instructions().append(src->index());
instructions().append(m_thisRegister.index());
return src;
}
return emitUnaryNoDstOp(op_ret, src);
}
RegisterID* BytecodeGenerator::emitUnaryNoDstOp(OpcodeID opcodeID, RegisterID* src)
{
emitOpcode(opcodeID);
instructions().append(src->index());
return src;
}
RegisterID* BytecodeGenerator::emitConstruct(RegisterID* dst, RegisterID* func, CallArguments& callArguments, unsigned divot, unsigned startOffset, unsigned endOffset)
{
ASSERT(func->refCount());
if (m_shouldEmitProfileHooks)
emitMove(callArguments.profileHookRegister(), func);
unsigned argumentIndex = 0;
if (ArgumentsNode* argumentsNode = callArguments.argumentsNode()) {
for (ArgumentListNode* n = argumentsNode->m_listNode; n; n = n->m_next)
emitNode(callArguments.argumentRegister(argumentIndex++), n);
}
if (m_shouldEmitProfileHooks) {
emitOpcode(op_profile_will_call);
instructions().append(callArguments.profileHookRegister()->index());
}
Vector<RefPtr<RegisterID>, RegisterFile::CallFrameHeaderSize> callFrame;
for (int i = 0; i < RegisterFile::CallFrameHeaderSize; ++i)
callFrame.append(newTemporary());
emitExpressionInfo(divot, startOffset, endOffset);
#if ENABLE(JIT)
m_codeBlock->addCallLinkInfo();
#endif
emitOpcode(op_construct);
instructions().append(func->index()); instructions().append(callArguments.count()); instructions().append(callArguments.callFrame()); if (dst != ignoredResult()) {
emitOpcode(op_call_put_result);
instructions().append(dst->index()); }
if (m_shouldEmitProfileHooks) {
emitOpcode(op_profile_did_call);
instructions().append(callArguments.profileHookRegister()->index());
}
return dst;
}
RegisterID* BytecodeGenerator::emitStrcat(RegisterID* dst, RegisterID* src, int count)
{
emitOpcode(op_strcat);
instructions().append(dst->index());
instructions().append(src->index());
instructions().append(count);
return dst;
}
void BytecodeGenerator::emitToPrimitive(RegisterID* dst, RegisterID* src)
{
emitOpcode(op_to_primitive);
instructions().append(dst->index());
instructions().append(src->index());
}
RegisterID* BytecodeGenerator::emitPushScope(RegisterID* scope)
{
ASSERT(scope->isTemporary());
ControlFlowContext context;
context.isFinallyBlock = false;
m_scopeContextStack.append(context);
m_dynamicScopeDepth++;
return emitUnaryNoDstOp(op_push_scope, scope);
}
void BytecodeGenerator::emitPopScope()
{
ASSERT(m_scopeContextStack.size());
ASSERT(!m_scopeContextStack.last().isFinallyBlock);
emitOpcode(op_pop_scope);
m_scopeContextStack.removeLast();
m_dynamicScopeDepth--;
}
void BytecodeGenerator::emitDebugHook(DebugHookID debugHookID, int firstLine, int lastLine)
{
#if ENABLE(DEBUG_WITH_BREAKPOINT)
if (debugHookID != DidReachBreakpoint)
return;
#else
if (!m_shouldEmitDebugHooks)
return;
#endif
emitOpcode(op_debug);
instructions().append(debugHookID);
instructions().append(firstLine);
instructions().append(lastLine);
}
void BytecodeGenerator::pushFinallyContext(Label* target, RegisterID* retAddrDst)
{
ControlFlowContext scope;
scope.isFinallyBlock = true;
FinallyContext context = { target, retAddrDst };
scope.finallyContext = context;
m_scopeContextStack.append(scope);
m_finallyDepth++;
}
void BytecodeGenerator::popFinallyContext()
{
ASSERT(m_scopeContextStack.size());
ASSERT(m_scopeContextStack.last().isFinallyBlock);
ASSERT(m_finallyDepth > 0);
m_scopeContextStack.removeLast();
m_finallyDepth--;
}
LabelScope* BytecodeGenerator::breakTarget(const Identifier& name)
{
while (m_labelScopes.size()) {
if (m_labelScopes.last().refCount())
break;
m_labelScopes.removeLast();
}
if (!m_labelScopes.size())
return 0;
if (name.isEmpty()) {
for (int i = m_labelScopes.size() - 1; i >= 0; --i) {
LabelScope* scope = &m_labelScopes[i];
if (scope->type() != LabelScope::NamedLabel) {
ASSERT(scope->breakTarget());
return scope;
}
}
return 0;
}
for (int i = m_labelScopes.size() - 1; i >= 0; --i) {
LabelScope* scope = &m_labelScopes[i];
if (scope->name() && *scope->name() == name) {
ASSERT(scope->breakTarget());
return scope;
}
}
return 0;
}
LabelScope* BytecodeGenerator::continueTarget(const Identifier& name)
{
while (m_labelScopes.size() && !m_labelScopes.last().refCount())
m_labelScopes.removeLast();
if (!m_labelScopes.size())
return 0;
if (name.isEmpty()) {
for (int i = m_labelScopes.size() - 1; i >= 0; --i) {
LabelScope* scope = &m_labelScopes[i];
if (scope->type() == LabelScope::Loop) {
ASSERT(scope->continueTarget());
return scope;
}
}
return 0;
}
LabelScope* result = 0;
for (int i = m_labelScopes.size() - 1; i >= 0; --i) {
LabelScope* scope = &m_labelScopes[i];
if (scope->type() == LabelScope::Loop) {
ASSERT(scope->continueTarget());
result = scope;
}
if (scope->name() && *scope->name() == name)
return result; }
return 0;
}
PassRefPtr<Label> BytecodeGenerator::emitComplexJumpScopes(Label* target, ControlFlowContext* topScope, ControlFlowContext* bottomScope)
{
while (topScope > bottomScope) {
int nNormalScopes = 0;
while (topScope > bottomScope) {
if (topScope->isFinallyBlock)
break;
++nNormalScopes;
--topScope;
}
if (nNormalScopes) {
size_t begin = instructions().size();
emitOpcode(op_jmp_scopes);
instructions().append(nNormalScopes);
if (topScope == bottomScope) {
instructions().append(target->bind(begin, instructions().size()));
return target;
}
RefPtr<Label> nextInsn = newLabel();
instructions().append(nextInsn->bind(begin, instructions().size()));
emitLabel(nextInsn.get());
}
while (topScope > bottomScope && topScope->isFinallyBlock) {
emitJumpSubroutine(topScope->finallyContext.retAddrDst, topScope->finallyContext.finallyAddr);
--topScope;
}
}
return emitJump(target);
}
PassRefPtr<Label> BytecodeGenerator::emitJumpScopes(Label* target, int targetScopeDepth)
{
ASSERT(scopeDepth() - targetScopeDepth >= 0);
ASSERT(target->isForward());
size_t scopeDelta = scopeDepth() - targetScopeDepth;
ASSERT(scopeDelta <= m_scopeContextStack.size());
if (!scopeDelta)
return emitJump(target);
if (m_finallyDepth)
return emitComplexJumpScopes(target, &m_scopeContextStack.last(), &m_scopeContextStack.last() - scopeDelta);
size_t begin = instructions().size();
emitOpcode(op_jmp_scopes);
instructions().append(scopeDelta);
instructions().append(target->bind(begin, instructions().size()));
return target;
}
RegisterID* BytecodeGenerator::emitGetPropertyNames(RegisterID* dst, RegisterID* base, RegisterID* i, RegisterID* size, Label* breakTarget)
{
size_t begin = instructions().size();
emitOpcode(op_get_pnames);
instructions().append(dst->index());
instructions().append(base->index());
instructions().append(i->index());
instructions().append(size->index());
instructions().append(breakTarget->bind(begin, instructions().size()));
return dst;
}
RegisterID* BytecodeGenerator::emitNextPropertyName(RegisterID* dst, RegisterID* base, RegisterID* i, RegisterID* size, RegisterID* iter, Label* target)
{
size_t begin = instructions().size();
emitOpcode(op_next_pname);
instructions().append(dst->index());
instructions().append(base->index());
instructions().append(i->index());
instructions().append(size->index());
instructions().append(iter->index());
instructions().append(target->bind(begin, instructions().size()));
return dst;
}
RegisterID* BytecodeGenerator::emitCatch(RegisterID* targetRegister, Label* start, Label* end)
{
m_usesExceptions = true;
#if ENABLE(JIT)
HandlerInfo info = { start->bind(0, 0), end->bind(0, 0), instructions().size(), m_dynamicScopeDepth + m_baseScopeDepth, CodeLocationLabel() };
#else
HandlerInfo info = { start->bind(0, 0), end->bind(0, 0), instructions().size(), m_dynamicScopeDepth + m_baseScopeDepth };
#endif
m_codeBlock->addExceptionHandler(info);
emitOpcode(op_catch);
instructions().append(targetRegister->index());
return targetRegister;
}
void BytecodeGenerator::emitThrowReferenceError(const UString& message)
{
emitOpcode(op_throw_reference_error);
instructions().append(addConstantValue(jsString(globalData(), message))->index());
}
PassRefPtr<Label> BytecodeGenerator::emitJumpSubroutine(RegisterID* retAddrDst, Label* finally)
{
size_t begin = instructions().size();
emitOpcode(op_jsr);
instructions().append(retAddrDst->index());
instructions().append(finally->bind(begin, instructions().size()));
emitLabel(newLabel().get()); return finally;
}
void BytecodeGenerator::emitSubroutineReturn(RegisterID* retAddrSrc)
{
emitOpcode(op_sret);
instructions().append(retAddrSrc->index());
}
void BytecodeGenerator::emitPushNewScope(RegisterID* dst, const Identifier& property, RegisterID* value)
{
ControlFlowContext context;
context.isFinallyBlock = false;
m_scopeContextStack.append(context);
m_dynamicScopeDepth++;
emitOpcode(op_push_new_scope);
instructions().append(dst->index());
instructions().append(addConstant(property));
instructions().append(value->index());
}
void BytecodeGenerator::beginSwitch(RegisterID* scrutineeRegister, SwitchInfo::SwitchType type)
{
SwitchInfo info = { instructions().size(), type };
switch (type) {
case SwitchInfo::SwitchImmediate:
emitOpcode(op_switch_imm);
break;
case SwitchInfo::SwitchCharacter:
emitOpcode(op_switch_char);
break;
case SwitchInfo::SwitchString:
emitOpcode(op_switch_string);
break;
default:
ASSERT_NOT_REACHED();
}
instructions().append(0); instructions().append(0); instructions().append(scrutineeRegister->index());
m_switchContextStack.append(info);
}
static int32_t keyForImmediateSwitch(ExpressionNode* node, int32_t min, int32_t max)
{
UNUSED_PARAM(max);
ASSERT(node->isNumber());
double value = static_cast<NumberNode*>(node)->value();
int32_t key = static_cast<int32_t>(value);
ASSERT(key == value);
ASSERT(key >= min);
ASSERT(key <= max);
return key - min;
}
static void prepareJumpTableForImmediateSwitch(SimpleJumpTable& jumpTable, int32_t switchAddress, uint32_t clauseCount, RefPtr<Label>* labels, ExpressionNode** nodes, int32_t min, int32_t max)
{
jumpTable.min = min;
jumpTable.branchOffsets.resize(max - min + 1);
jumpTable.branchOffsets.fill(0);
for (uint32_t i = 0; i < clauseCount; ++i) {
ASSERT(!labels[i]->isForward());
jumpTable.add(keyForImmediateSwitch(nodes[i], min, max), labels[i]->bind(switchAddress, switchAddress + 3));
}
}
static int32_t keyForCharacterSwitch(ExpressionNode* node, int32_t min, int32_t max)
{
UNUSED_PARAM(max);
ASSERT(node->isString());
StringImpl* clause = static_cast<StringNode*>(node)->value().impl();
ASSERT(clause->length() == 1);
int32_t key = clause->characters()[0];
ASSERT(key >= min);
ASSERT(key <= max);
return key - min;
}
static void prepareJumpTableForCharacterSwitch(SimpleJumpTable& jumpTable, int32_t switchAddress, uint32_t clauseCount, RefPtr<Label>* labels, ExpressionNode** nodes, int32_t min, int32_t max)
{
jumpTable.min = min;
jumpTable.branchOffsets.resize(max - min + 1);
jumpTable.branchOffsets.fill(0);
for (uint32_t i = 0; i < clauseCount; ++i) {
ASSERT(!labels[i]->isForward());
jumpTable.add(keyForCharacterSwitch(nodes[i], min, max), labels[i]->bind(switchAddress, switchAddress + 3));
}
}
static void prepareJumpTableForStringSwitch(StringJumpTable& jumpTable, int32_t switchAddress, uint32_t clauseCount, RefPtr<Label>* labels, ExpressionNode** nodes)
{
for (uint32_t i = 0; i < clauseCount; ++i) {
ASSERT(!labels[i]->isForward());
ASSERT(nodes[i]->isString());
StringImpl* clause = static_cast<StringNode*>(nodes[i])->value().impl();
OffsetLocation location;
location.branchOffset = labels[i]->bind(switchAddress, switchAddress + 3);
jumpTable.offsetTable.add(clause, location);
}
}
void BytecodeGenerator::endSwitch(uint32_t clauseCount, RefPtr<Label>* labels, ExpressionNode** nodes, Label* defaultLabel, int32_t min, int32_t max)
{
SwitchInfo switchInfo = m_switchContextStack.last();
m_switchContextStack.removeLast();
if (switchInfo.switchType == SwitchInfo::SwitchImmediate) {
instructions()[switchInfo.bytecodeOffset + 1] = m_codeBlock->numberOfImmediateSwitchJumpTables();
instructions()[switchInfo.bytecodeOffset + 2] = defaultLabel->bind(switchInfo.bytecodeOffset, switchInfo.bytecodeOffset + 3);
SimpleJumpTable& jumpTable = m_codeBlock->addImmediateSwitchJumpTable();
prepareJumpTableForImmediateSwitch(jumpTable, switchInfo.bytecodeOffset, clauseCount, labels, nodes, min, max);
} else if (switchInfo.switchType == SwitchInfo::SwitchCharacter) {
instructions()[switchInfo.bytecodeOffset + 1] = m_codeBlock->numberOfCharacterSwitchJumpTables();
instructions()[switchInfo.bytecodeOffset + 2] = defaultLabel->bind(switchInfo.bytecodeOffset, switchInfo.bytecodeOffset + 3);
SimpleJumpTable& jumpTable = m_codeBlock->addCharacterSwitchJumpTable();
prepareJumpTableForCharacterSwitch(jumpTable, switchInfo.bytecodeOffset, clauseCount, labels, nodes, min, max);
} else {
ASSERT(switchInfo.switchType == SwitchInfo::SwitchString);
instructions()[switchInfo.bytecodeOffset + 1] = m_codeBlock->numberOfStringSwitchJumpTables();
instructions()[switchInfo.bytecodeOffset + 2] = defaultLabel->bind(switchInfo.bytecodeOffset, switchInfo.bytecodeOffset + 3);
StringJumpTable& jumpTable = m_codeBlock->addStringSwitchJumpTable();
prepareJumpTableForStringSwitch(jumpTable, switchInfo.bytecodeOffset, clauseCount, labels, nodes);
}
}
RegisterID* BytecodeGenerator::emitThrowExpressionTooDeepException()
{
m_expressionTooDeep = true;
return newTemporary();
}
void BytecodeGenerator::setIsNumericCompareFunction(bool isNumericCompareFunction)
{
m_codeBlock->setIsNumericCompareFunction(isNumericCompareFunction);
}
int BytecodeGenerator::argumentNumberFor(const Identifier& ident)
{
int parameterCount = m_parameters.size(); RegisterID* registerID = registerFor(ident);
if (!registerID)
return 0;
int index = registerID->index() + RegisterFile::CallFrameHeaderSize + parameterCount;
return (index > 0 && index < parameterCount) ? index : 0;
}
}