MCPcopy Create free account
hub / github.com/Vector35/binaryninja-api / normalize

Function normalize

arch/arm64/disassembler/disasm_test.py:19–77  ·  view source on GitHub ↗
(instxt)

Source from the content-addressed store, hash-verified

17pfunc_disasm = None
18
19def normalize(instxt):
20 #print('normalizing: %s' % instxt)
21 instxt = instxt.strip()
22
23 # collapse runs of whitespace to one space character
24 instxt = re.sub(r'\s+', ' ', instxt)
25
26 # remove comments
27 if ' //' in instxt:
28 instxt = instxt[0:instxt.find(' //')]
29
30 # change that range notation
31 # st4w {z14.s-z17.s}, p2, [x11, x19, lsl #2]
32 # ->
33 # st4w {z14.s, z15.s, z16.s, z17.s}, p2, [x11, x19, lsl #2]
34 m = re.search(r'{z(\d+)\.(.)-z(\d+)\.(.)}', instxt)
35 if m:
36 (lhs, suffix_a, rhs, suffix_b) = m.group(1,2,3,4)
37 assert suffix_a == suffix_b
38 (lhs, rhs) = (int(lhs), int(rhs))
39 if rhs-lhs+1 == 4:
40 replacement = '{z%d.%s, z%d.%s, z%d.%s, z%d.%s}' % \
41 (lhs, suffix_a, (lhs+1)%32, suffix_a, (lhs+2)%32, suffix_a, (lhs+3)%32, suffix_a)
42 elif rhs-lhs+1 == 3:
43 replacement = '{z%d.%s, z%d.%s, z%d.%s}' % \
44 (lhs, suffix_a, (lhs+1)%32, suffix_a, (lhs+2)%32, suffix_a)
45 instxt = instxt.replace(m.group(0), replacement)
46
47 # remove spaces from { list }
48 # eg: { v5.b, v6.b, v7.b, v8.b } -> {v5.b, v6.b, v7.b, v8.b}
49 instxt = re.sub(r'{ (.*?) }', r'{\1}', instxt)
50
51 # remove leading hex zeros
52 # 0x00000000071eb000 -> 0x71eb000
53 instxt = re.sub(r'0x00+', r'0x', instxt)
54
55 # decimal immediates to hex
56 # add x29, x15, x25, lsl #6 -> add x29, x15, x25, lsl #0x6
57 for dec_imm in re.findall(r'#\d+[,\]]', instxt):
58 hex_imm = '#0x%x' % int(dec_imm[1:-1]) + dec_imm[-1]
59 instxt = instxt.replace(dec_imm, hex_imm, 1)
60 for dec_imm in re.findall(r'#\d+$', instxt):
61 if not instxt.endswith(dec_imm): continue
62 hex_imm = '#0x%x' % int(dec_imm[1:])
63 instxt = instxt[0:-len(dec_imm)] + hex_imm
64
65 # #-3.375000000000000000e+00 -> #-3.375
66 for x in re.findall(r'#[-\+\.\de]{8,}', instxt):
67 instxt = instxt.replace(x, '#'+str(float(x[1:])))
68
69 # 0x0.000000 -> 0x0.0
70 instxt = instxt.replace('0.000000', '0.0')
71 instxt = instxt.replace('0.000', '0.0')
72
73 # lowercase everything
74 instxt = instxt.lower()
75
76 # done

Callers 1

disassembleFunction · 0.85

Calls 4

findMethod · 0.80
searchMethod · 0.80
subMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected