export function findMnemonicFrom(instructions, start, mnemonic) {
for (let i = start; i < instructions.length; i++) {
if (instructions[i].opcode.mnemonic === mnemonic) return i;
}
return -1;
}
export function findTryFrom(instructions, start) {
for (let i = start; i < instructions.length; i++) {
const mnemonic = instructions[i].opcode.mnemonic;
if (mnemonic === "TRY" || mnemonic === "TRY_L") return i;
}
return -1;
}
export function tryHandlerTargets(instruction) {
if (instruction.opcode.mnemonic !== "TRY" && instruction.opcode.mnemonic !== "TRY_L") {
return null;
}
const operand = instruction.operand;
if (operand === null || operand.kind !== "Bytes") {
return null;
}
const bytes = operand.value;
let catchDelta;
let finallyDelta;
if (bytes.length === 2) {
catchDelta = bytes[0];
finallyDelta = bytes[1];
if (catchDelta > 127) catchDelta -= 256;
if (finallyDelta > 127) finallyDelta -= 256;
} else if (bytes.length === 8) {
catchDelta = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
finallyDelta = bytes[4] | (bytes[5] << 8) | (bytes[6] << 16) | (bytes[7] << 24);
if (catchDelta > 2147483647) catchDelta -= 4294967296;
if (finallyDelta > 2147483647) finallyDelta -= 4294967296;
} else {
return null;
}
const width = 1 + bytes.length;
const bodyStart = instruction.offset + width;
let catchTarget = null;
if (catchDelta !== 0) {
const target = instruction.offset + catchDelta;
if (target > instruction.offset) {
catchTarget = target;
}
}
let finallyTarget = null;
if (finallyDelta !== 0) {
const target = instruction.offset + finallyDelta;
if (target > instruction.offset) {
finallyTarget = target;
}
}
return { bodyStart, catchTarget, finallyTarget };
}