1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/*
* Copyright (C) 2016-2021 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "BytecodeGenerator.h"
#include "BytecodeStructs.h"
#include "InterpreterInlines.h"
#include "Opcode.h"
#include "PreciseJumpTargets.h"
namespace JSC {
#define SWITCH_JMP(CASE_OP, SWITCH_CASE) \
switch (instruction->opcodeID()) { \
CASE_OP(OpJmp) \
\
CASE_OP(OpJtrue) \
CASE_OP(OpJfalse) \
CASE_OP(OpJeqNull) \
CASE_OP(OpJneqNull) \
CASE_OP(OpJundefinedOrNull) \
CASE_OP(OpJnundefinedOrNull) \
CASE_OP(OpJeqPtr) \
CASE_OP(OpJneqPtr) \
\
CASE_OP(OpJless) \
CASE_OP(OpJlesseq) \
CASE_OP(OpJgreater) \
CASE_OP(OpJgreatereq) \
CASE_OP(OpJnless) \
CASE_OP(OpJnlesseq) \
CASE_OP(OpJngreater) \
CASE_OP(OpJngreatereq) \
CASE_OP(OpJeq) \
CASE_OP(OpJneq) \
CASE_OP(OpJstricteq) \
CASE_OP(OpJnstricteq) \
CASE_OP(OpJbelow) \
CASE_OP(OpJbeloweq) \
case op_switch_imm: { \
auto bytecode = instruction->as<OpSwitchImm>(); \
auto& table = codeBlock->unlinkedSwitchJumpTable(bytecode.m_tableIndex); \
for (unsigned i = table.m_branchOffsets.size(); i--;) \
SWITCH_CASE(table.m_branchOffsets[i]); \
SWITCH_CASE(table.m_defaultOffset); \
break; \
} \
case op_switch_char: { \
auto bytecode = instruction->as<OpSwitchChar>(); \
auto& table = codeBlock->unlinkedSwitchJumpTable(bytecode.m_tableIndex); \
for (unsigned i = table.m_branchOffsets.size(); i--;) \
SWITCH_CASE(table.m_branchOffsets[i]); \
SWITCH_CASE(table.m_defaultOffset); \
break; \
} \
case op_switch_string: { \
auto bytecode = instruction->as<OpSwitchString>(); \
auto& table = codeBlock->unlinkedStringSwitchJumpTable(bytecode.m_tableIndex); \
for (auto& entry : table.m_offsetTable) \
SWITCH_CASE(entry.value.m_branchOffset); \
SWITCH_CASE(table.m_defaultOffset); \
break; \
} \
default: \
break; \
} \
template<typename Block>
inline int jumpTargetForInstruction(Block* codeBlock, const JSInstructionStream::Ref& instruction, unsigned target)
{
if (target)
return target;
return codeBlock->outOfLineJumpOffset(instruction);
}
template<typename UncheckedKeyHashMap>
inline int jumpTargetForInstruction(UncheckedKeyHashMap& outOfLineJumpTargets, const JSInstructionStream::Ref& instruction, unsigned target)
{
if (target)
return target;
ASSERT(outOfLineJumpTargets.contains(instruction.offset()));
return outOfLineJumpTargets.get(instruction.offset());
}
template<typename Op, typename Block>
inline int jumpTargetForInstruction(Block&& codeBlock, const JSInstructionStream::Ref& instruction)
{
auto bytecode = instruction->as<Op>();
return jumpTargetForInstruction(codeBlock, instruction, bytecode.m_targetLabel);
}
template<typename Block, typename Function>
inline void extractStoredJumpTargetsForInstruction(Block&& codeBlock, const JSInstructionStream::Ref& instruction, NOESCAPE const Function& function)
{
#define CASE_OP(__op) \
case __op::opcodeID: \
function(jumpTargetForInstruction<__op>(codeBlock, instruction)); \
break;
#define SWITCH_CASE(__target) \
function(__target)
SWITCH_JMP(CASE_OP, SWITCH_CASE)
#undef CASE_OP
#undef SWITCH_CASE
}
template<typename Block, typename Function, typename CodeBlockOrHashMap>
inline void updateStoredJumpTargetsForInstruction(Block&& codeBlock, unsigned finalOffset, JSInstructionStream::MutableRef instruction, NOESCAPE const Function& function, CodeBlockOrHashMap& codeBlockOrHashMap)
{
#define CASE_OP(__op) \
case __op::opcodeID: { \
int32_t target = jumpTargetForInstruction<__op>(codeBlockOrHashMap, instruction); \
int32_t newTarget = function(target); \
instruction->cast<__op>()->setTargetLabel(BoundLabel(newTarget), [&]() { \
codeBlock->addOutOfLineJumpTarget(finalOffset + instruction.offset(), newTarget); \
return BoundLabel(); \
}); \
break; \
}
#define SWITCH_CASE(__target) \
do { \
int32_t target = __target; \
__target = function(target); \
} while (false)
SWITCH_JMP(CASE_OP, SWITCH_CASE)
#undef CASE_OP
#undef JMP_TARGET
}
template<typename Block, typename Function>
inline void updateStoredJumpTargetsForInstruction(Block* codeBlock, unsigned finalOffset, JSInstructionStream::MutableRef instruction, Function function)
{
updateStoredJumpTargetsForInstruction(codeBlock, finalOffset, instruction, function, codeBlock);
}
} // namespace JSC
|