#!/usr/bin/env python3 # run disassembly tests in test_cases.txt # ensure you built gofer.so (see Makefile-local) import os, sys, struct, ctypes, re RED = '\x1B[31m' GREEN = '\x1B[32m' NORMAL = '\x1B[0m' #------------------------------------------------------------------------------ # disassemble #------------------------------------------------------------------------------ cbuf = None pfunc_disasm = None def normalize(instxt): #print('normalizing: %s' % instxt) instxt = instxt.strip() # collapse runs of whitespace to one space character instxt = re.sub('\s+', ' ', instxt) # remove comments if ' //' in instxt: instxt = instxt[0:instxt.find(' //')] # change that range notation # st4w {z14.s-z17.s}, p2, [x11, x19, lsl #2] # -> # st4w {z14.s, z15.s, z16.s, z17.s}, p2, [x11, x19, lsl #2] m = re.search(r'{z(\d+)\.(.)-z(\d+)\.(.)}', instxt) if m: (lhs, suffix_a, rhs, suffix_b) = m.group(1,2,3,4) assert suffix_a == suffix_b (lhs, rhs) = (int(lhs), int(rhs)) if rhs-lhs+1 == 4: replacement = '{z%d.%s, z%d.%s, z%d.%s, z%d.%s}' % \ (lhs, suffix_a, (lhs+1)%32, suffix_a, (lhs+2)%32, suffix_a, (lhs+3)%32, suffix_a) elif rhs-lhs+1 == 3: replacement = '{z%d.%s, z%d.%s, z%d.%s}' % \ (lhs, suffix_a, (lhs+1)%32, suffix_a, (lhs+2)%32, suffix_a) instxt = instxt.replace(m.group(0), replacement) # remove spaces from { list } # eg: { v5.b, v6.b, v7.b, v8.b } -> {v5.b, v6.b, v7.b, v8.b} instxt = re.sub(r'{ (.*?) }', r'{\1}', instxt) # remove leading hex zeros # 0x00000000071eb000 -> 0x71eb000 instxt = re.sub(r'0x00+', r'0x', instxt) # decimal immediates to hex # add x29, x15, x25, lsl #6 -> add x29, x15, x25, lsl #0x6 for dec_imm in re.findall(r'#\d+[,\]]', instxt): hex_imm = '#0x%x' % int(dec_imm[1:-1]) + dec_imm[-1] instxt = instxt.replace(dec_imm, hex_imm, 1) for dec_imm in re.findall(r'#\d+$', instxt): if not instxt.endswith(dec_imm): continue hex_imm = '#0x%x' % int(dec_imm[1:]) instxt = instxt[0:-len(dec_imm)] + hex_imm # #-3.375000000000000000e+00 -> #-3.375 for x in re.findall(r'#[-\+\.\de]{8,}', instxt): instxt = instxt.replace(x, '#'+str(float(x[1:]))) # 0x0.000000 -> 0x0.0 instxt = instxt.replace('0.000000', '0.0') instxt = instxt.replace('0.000', '0.0') # lowercase everything instxt = instxt.lower() # done return instxt def disassemble(addr, insnum): global cbuf, pfunc_disasm insword = struct.pack('