aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-cov/src/instructions.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-03-05 11:05:26 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-03-12 10:37:27 +0800
commit13299158d1e22d1af1cd36434fc403a74743ecb1 (patch)
tree9b35435c6f8641d2dc3d7bfd530c7c4f040a1f51 /packages/sol-cov/src/instructions.ts
parenta6571b09d2087ffb9a4860c0db3d7344321fe2c3 (diff)
downloaddexon-sol-tools-13299158d1e22d1af1cd36434fc403a74743ecb1.tar
dexon-sol-tools-13299158d1e22d1af1cd36434fc403a74743ecb1.tar.gz
dexon-sol-tools-13299158d1e22d1af1cd36434fc403a74743ecb1.tar.bz2
dexon-sol-tools-13299158d1e22d1af1cd36434fc403a74743ecb1.tar.lz
dexon-sol-tools-13299158d1e22d1af1cd36434fc403a74743ecb1.tar.xz
dexon-sol-tools-13299158d1e22d1af1cd36434fc403a74743ecb1.tar.zst
dexon-sol-tools-13299158d1e22d1af1cd36434fc403a74743ecb1.zip
Add sol-cover implementation
Diffstat (limited to 'packages/sol-cov/src/instructions.ts')
-rw-r--r--packages/sol-cov/src/instructions.ts24
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;
+};