diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2019-01-10 18:21:05 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2019-01-10 18:21:05 +0800 |
commit | 7ae9e79235ed3b7eb110b0a1e88338b3965f44da (patch) | |
tree | 5339e9548142eb6ec3e7a7e4288cb5e5b1a582cc /packages/sol-tracing-utils/src/instructions.ts | |
parent | 15c9479ebeca57e7c275cd2e73ca3daad03a412f (diff) | |
download | dexon-0x-contracts-7ae9e79235ed3b7eb110b0a1e88338b3965f44da.tar.gz dexon-0x-contracts-7ae9e79235ed3b7eb110b0a1e88338b3965f44da.tar.zst dexon-0x-contracts-7ae9e79235ed3b7eb110b0a1e88338b3965f44da.zip |
Rename sol-trace-based-tools-common to sol-tracing-utils
Diffstat (limited to 'packages/sol-tracing-utils/src/instructions.ts')
-rw-r--r-- | packages/sol-tracing-utils/src/instructions.ts | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/packages/sol-tracing-utils/src/instructions.ts b/packages/sol-tracing-utils/src/instructions.ts new file mode 100644 index 000000000..40987dbe5 --- /dev/null +++ b/packages/sol-tracing-utils/src/instructions.ts @@ -0,0 +1,23 @@ +import { constants } from './constants'; + +const isPush = (inst: number) => inst >= constants.PUSH1 && inst <= constants.PUSH32; + +const pushDataLength = (inst: number) => inst - constants.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; +}; |