UnlinkedCodeBlockGenerator.cpp [plain text]
#include "config.h"
#include "UnlinkedCodeBlockGenerator.h"
#include "BytecodeRewriter.h"
#include "InstructionStream.h"
#include "JSCJSValueInlines.h"
#include "PreciseJumpTargets.h"
#include "UnlinkedMetadataTableInlines.h"
namespace JSC {
inline void UnlinkedCodeBlockGenerator::getLineAndColumn(const ExpressionRangeInfo& info, unsigned& line, unsigned& column) const
{
switch (info.mode) {
case ExpressionRangeInfo::FatLineMode:
info.decodeFatLineMode(line, column);
break;
case ExpressionRangeInfo::FatColumnMode:
info.decodeFatColumnMode(line, column);
break;
case ExpressionRangeInfo::FatLineAndColumnMode: {
unsigned fatIndex = info.position;
const ExpressionRangeInfo::FatPosition& fatPos = m_expressionInfoFatPositions[fatIndex];
line = fatPos.line;
column = fatPos.column;
break;
}
} }
void UnlinkedCodeBlockGenerator::addExpressionInfo(unsigned instructionOffset, int divot, int startOffset, int endOffset, unsigned line, unsigned column)
{
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;
}
unsigned positionMode =
(line <= ExpressionRangeInfo::MaxFatLineModeLine && column <= ExpressionRangeInfo::MaxFatLineModeColumn)
? ExpressionRangeInfo::FatLineMode
: (line <= ExpressionRangeInfo::MaxFatColumnModeLine && column <= ExpressionRangeInfo::MaxFatColumnModeColumn)
? ExpressionRangeInfo::FatColumnMode
: ExpressionRangeInfo::FatLineAndColumnMode;
ExpressionRangeInfo info;
info.instructionOffset = instructionOffset;
info.divotPoint = divot;
info.startOffset = startOffset;
info.endOffset = endOffset;
info.mode = positionMode;
switch (positionMode) {
case ExpressionRangeInfo::FatLineMode:
info.encodeFatLineMode(line, column);
break;
case ExpressionRangeInfo::FatColumnMode:
info.encodeFatColumnMode(line, column);
break;
case ExpressionRangeInfo::FatLineAndColumnMode: {
unsigned fatIndex = m_expressionInfoFatPositions.size();
ExpressionRangeInfo::FatPosition fatPos = { line, column };
m_expressionInfoFatPositions.append(fatPos);
info.position = fatIndex;
}
}
m_expressionInfo.append(info);
}
void UnlinkedCodeBlockGenerator::addTypeProfilerExpressionInfo(unsigned instructionOffset, unsigned startDivot, unsigned endDivot)
{
UnlinkedCodeBlock::RareData::TypeProfilerExpressionRange range;
range.m_startDivot = startDivot;
range.m_endDivot = endDivot;
m_typeProfilerInfoMap.set(instructionOffset, range);
}
void UnlinkedCodeBlockGenerator::finalize(std::unique_ptr<InstructionStream> instructions)
{
ASSERT(instructions);
{
auto locker = holdLock(m_codeBlock->cellLock());
m_codeBlock->m_instructions = WTFMove(instructions);
m_codeBlock->m_metadata->finalize();
m_codeBlock->m_jumpTargets = WTFMove(m_jumpTargets);
m_codeBlock->m_identifiers = WTFMove(m_identifiers);
m_codeBlock->m_constantRegisters = WTFMove(m_constantRegisters);
m_codeBlock->m_constantsSourceCodeRepresentation = WTFMove(m_constantsSourceCodeRepresentation);
m_codeBlock->m_functionDecls = WTFMove(m_functionDecls);
m_codeBlock->m_functionExprs = WTFMove(m_functionExprs);
m_codeBlock->m_expressionInfo = WTFMove(m_expressionInfo);
m_codeBlock->m_outOfLineJumpTargets = WTFMove(m_outOfLineJumpTargets);
if (!m_codeBlock->m_rareData) {
if (!m_exceptionHandlers.isEmpty()
|| !m_switchJumpTables.isEmpty()
|| !m_stringSwitchJumpTables.isEmpty()
|| !m_expressionInfoFatPositions.isEmpty()
|| !m_typeProfilerInfoMap.isEmpty()
|| !m_opProfileControlFlowBytecodeOffsets.isEmpty()
|| !m_bitVectors.isEmpty()
|| !m_constantIdentifierSets.isEmpty())
m_codeBlock->createRareDataIfNecessary(locker);
}
if (m_codeBlock->m_rareData) {
m_codeBlock->m_rareData->m_exceptionHandlers = WTFMove(m_exceptionHandlers);
m_codeBlock->m_rareData->m_switchJumpTables = WTFMove(m_switchJumpTables);
m_codeBlock->m_rareData->m_stringSwitchJumpTables = WTFMove(m_stringSwitchJumpTables);
m_codeBlock->m_rareData->m_expressionInfoFatPositions = WTFMove(m_expressionInfoFatPositions);
m_codeBlock->m_rareData->m_typeProfilerInfoMap = WTFMove(m_typeProfilerInfoMap);
m_codeBlock->m_rareData->m_opProfileControlFlowBytecodeOffsets = WTFMove(m_opProfileControlFlowBytecodeOffsets);
m_codeBlock->m_rareData->m_bitVectors = WTFMove(m_bitVectors);
m_codeBlock->m_rareData->m_constantIdentifierSets = WTFMove(m_constantIdentifierSets);
}
}
m_vm.heap.writeBarrier(m_codeBlock.get());
m_vm.heap.reportExtraMemoryAllocated(m_codeBlock->m_instructions->sizeInBytes() + m_codeBlock->m_metadata->sizeInBytes());
}
UnlinkedHandlerInfo* UnlinkedCodeBlockGenerator::handlerForBytecodeIndex(BytecodeIndex bytecodeIndex, RequiredHandler requiredHandler)
{
return handlerForIndex(bytecodeIndex.offset(), requiredHandler);
}
UnlinkedHandlerInfo* UnlinkedCodeBlockGenerator::handlerForIndex(unsigned index, RequiredHandler requiredHandler)
{
return UnlinkedHandlerInfo::handlerForIndex<UnlinkedHandlerInfo>(m_exceptionHandlers, index, requiredHandler);
}
void UnlinkedCodeBlockGenerator::applyModification(BytecodeRewriter& rewriter, InstructionStreamWriter& instructions)
{
rewriter.adjustJumpTargets();
for (UnlinkedHandlerInfo& handler : m_exceptionHandlers) {
handler.target = rewriter.adjustAbsoluteOffset(handler.target);
handler.start = rewriter.adjustAbsoluteOffset(handler.start);
handler.end = rewriter.adjustAbsoluteOffset(handler.end);
}
for (size_t i = 0; i < m_opProfileControlFlowBytecodeOffsets.size(); ++i)
m_opProfileControlFlowBytecodeOffsets[i] = rewriter.adjustAbsoluteOffset(m_opProfileControlFlowBytecodeOffsets[i]);
if (!m_typeProfilerInfoMap.isEmpty()) {
HashMap<unsigned, UnlinkedCodeBlock::RareData::TypeProfilerExpressionRange> adjustedTypeProfilerInfoMap;
for (auto& entry : m_typeProfilerInfoMap)
adjustedTypeProfilerInfoMap.set(rewriter.adjustAbsoluteOffset(entry.key), entry.value);
m_typeProfilerInfoMap.swap(adjustedTypeProfilerInfoMap);
}
for (size_t i = 0; i < m_expressionInfo.size(); ++i)
m_expressionInfo[i].instructionOffset = rewriter.adjustAbsoluteOffset(m_expressionInfo[i].instructionOffset);
rewriter.applyModification();
m_jumpTargets.clear();
recomputePreciseJumpTargets(this, instructions, m_jumpTargets);
}
void UnlinkedCodeBlockGenerator::addOutOfLineJumpTarget(InstructionStream::Offset bytecodeOffset, int target)
{
RELEASE_ASSERT(target);
m_outOfLineJumpTargets.set(bytecodeOffset, target);
}
int UnlinkedCodeBlockGenerator::outOfLineJumpOffset(InstructionStream::Offset bytecodeOffset)
{
ASSERT(m_outOfLineJumpTargets.contains(bytecodeOffset));
return m_outOfLineJumpTargets.get(bytecodeOffset);
}
void UnlinkedCodeBlockGenerator::dump(PrintStream&) const
{
}
}