diff options
Diffstat (limited to 'packages/sol-cov/src/instructions.ts')
-rw-r--r-- | packages/sol-cov/src/instructions.ts | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/packages/sol-cov/src/instructions.ts b/packages/sol-cov/src/instructions.ts new file mode 100644 index 000000000..c6506e58d --- /dev/null +++ b/packages/sol-cov/src/instructions.ts @@ -0,0 +1,24 @@ +// tslint:disable:number-literal-format +const PUSH1 = 0x60; +const PUSH32 = 0x7f; +const isPush = (inst: number) => inst >= PUSH1 && inst <= PUSH32; + +const pushDataLength = (inst: number) => inst - PUSH1 + 1; + +const instructionLength = (inst: number) => (isPush(inst) ? pushDataLength(inst) + 1 : 1); + +export const getPcToInstructionIndexMapping = (bytecode: Uint8Array) => { + const result: { + [programCounter: number]: number; + } = {}; + let byteIndex = 0; + let instructionIndex = 0; + while (byteIndex < bytecode.length) { + const instruction = bytecode[byteIndex]; + const length = instructionLength(instruction); + result[byteIndex] = instructionIndex; + byteIndex += length; + instructionIndex += 1; + } + return result; +}; |