Skip to content

Commit

Permalink
Merge pull request #840 from tonlabs/find-dup
Browse files Browse the repository at this point in the history
Add find-dup.py tool
  • Loading branch information
mskvortsov authored Jan 24, 2023
2 parents ccfe9fd + 43130ff commit 6e67a90
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license-file = 'LICENSE.md'
name = 'tvm_linker'
readme = 'README.md'
repository = 'https://github.com/tonlabs/TVM-linker'
version = '0.19.0'
version = '0.19.1'

[[bin]]
name = 'tvm_linker'
Expand Down
50 changes: 50 additions & 0 deletions find-dup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Find duplicate fragments of linear sequences of code

# Sample invocation:
# 1. extract code boc into the input.boc with tvm_linker decode
# 2. tvm_linker disasm text --raw input.boc >input.code
# 3. python3 find-dup.py input.code

import sys

if len(sys.argv) != 2:
print("Usage: find-dup.py input.code")
exit(1)

size_low = 5
size_high = 101
size_width = len(str(size_high))

file = open(sys.argv[1], "r")
lines = file.readlines()
lines = [line.strip() for line in lines]
width = len(str(len(lines)))

def is_valid(pattern):
for line in pattern:
s = line.split(";")[0].strip()
if s.endswith("{") or s.startswith("}"):
return False
return True

for n in range(size_low, size_high):
matches = {}

for i in range(len(lines) - n + 1):
pattern = lines[i:i + n]
if not is_valid(pattern):
continue
for j in range(i + 1, len(lines) - n + 1):
fragment = lines[j:j + n]
if fragment == pattern:
if i not in matches:
matches.update({i: []})
l = matches[i]
l.append(j)

for i, js in matches.items():
if len(js) > 1:
s = "{n:0{w1}}: {i:0{w2}}".format(n = n, w1 = size_width, i = i, w2 = width)
for j in js:
s += " {j:0{w}}".format(j = j, w = width)
print(s)
2 changes: 1 addition & 1 deletion src/disasm/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ fn disasm_text_command(m: &ArgMatches) -> Status {
let mut roots = deserialize_cells_tree(&mut csor).map_err(|e| format_err!("{}", e))?;

if m.is_present("RAW") {
println!("{}", disasm_ex(&mut SliceData::load_cell_ref(roots.get(0).unwrap())?, true));
print!("{}", disasm_ex(&mut SliceData::load_cell_ref(roots.get(0).unwrap())?, true));
return Ok(())
}

Expand Down

0 comments on commit 6e67a90

Please sign in to comment.