aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-trace-based-tools-common/src/instructions.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2019-01-08 19:23:33 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2019-01-08 21:48:06 +0800
commit2c974b5f3ffa0e9736000273e39cdeee4a251b94 (patch)
treea1772f93d796e3b4ba7a988194a44a3e8bcd6d31 /packages/sol-trace-based-tools-common/src/instructions.ts
parent0ac36cef288deecd36caa601c53d13517eef5ca8 (diff)
downloaddexon-sol-tools-2c974b5f3ffa0e9736000273e39cdeee4a251b94.tar
dexon-sol-tools-2c974b5f3ffa0e9736000273e39cdeee4a251b94.tar.gz
dexon-sol-tools-2c974b5f3ffa0e9736000273e39cdeee4a251b94.tar.bz2
dexon-sol-tools-2c974b5f3ffa0e9736000273e39cdeee4a251b94.tar.lz
dexon-sol-tools-2c974b5f3ffa0e9736000273e39cdeee4a251b94.tar.xz
dexon-sol-tools-2c974b5f3ffa0e9736000273e39cdeee4a251b94.tar.zst
dexon-sol-tools-2c974b5f3ffa0e9736000273e39cdeee4a251b94.zip
Refactor out sol-cov, sol-profiler and sol-trace into their separate packages
Diffstat (limited to 'packages/sol-trace-based-tools-common/src/instructions.ts')
-rw-r--r--packages/sol-trace-based-tools-common/src/instructions.ts23
1 files changed, 23 insertions, 0 deletions
diff --git a/packages/sol-trace-based-tools-common/src/instructions.ts b/packages/sol-trace-based-tools-common/src/instructions.ts
new file mode 100644
index 000000000..40987dbe5
--- /dev/null
+++ b/packages/sol-trace-based-tools-common/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;
+};