inspectfp.py 2.69 KB
#!/usr/bin/python

# inspectfp.py <input_file>

import os
import sys

# search the following instructions in the input file
tab = {"double"     : ["faddd", "fsubd", "fmuld", "fdivd", "fsqrtd", "fcmdd", "fcmped","fitod","fdtoi","fdtos"],
       "single"     : ["fadds", "fsubs", "fmuls", "fdivs", "fsqrts", "fcmps", "fcmpes","fitos","fstoi","fstod","fsmuld","fabss","fnegs","fmovs"],
       "half"       : ["faddh", "fsubh", "fmulh", "fdivh", "fsqrth", "fcmph", "fcmpeh","fitoh","fhtoi","fhtos","fhmuls","fabsh","fmovh"],
       "packhalf"   : ["faddph","fsubph","fmulph","fdivph","fsqrtph","fcmpph","fcmpeph","fswaph","fmovhu","fmovhl","fmovhul","fmovhlu","fmovhzu","fmovhzl"],
       "packsingle" : ["faddps","fsubps","fmulps","fdivps","fsqrtps","fcmpps","fcmpeps"],
       "others"     : ["ldh","sth"],
      }


if len(sys.argv)<2:
  print("E: Missing argument. Script needs one argument - input file.")
  print("E: The optional second argument contains format ('tab'/'list')")
  sys.exit(1)

ctab = []
taby = 0
tabx = 0
for p in tab:
  if len(tab[p])>tabx:
    tabx = len(tab[p])
  ctab.append([0] * len(tab[p]))
  taby += 1


with open(sys.argv[1], 'r') as fi:
  ctx = fi.readlines()
  for ln in ctx:
    ty = 0
    for p in tab:
      for i in range(0,len(tab[p])):
        # instructions are placed after <tab> and separated with <dpace> in disassembler
        if ln.find("\t"+tab[p][i]+" ")>0:
          ctab[ty][i] += 1
      ty += 1

fmt = 'tab'
if len(sys.argv)>2:
  fmt = sys.argv[2]

if fmt=='tab':
  ty=0
  print("===== Table of FP instructions ==========================================")
  if tabx>6:
    for p in tab:
      ln = "{:10} ".format(p)
      for i in range(0,6):
        if (i<len(tab[p])):
          ln += "| {:7}: {:4}".format(tab[p][i], ctab[ty][i])
      print(ln)
      ty += 1
    print("-------------------------------------------------------------------------")
    ty = 0
    for p in tab:
      ln = "{:10} ".format(p)
      for i in range(6,len(tab[p])):
        if (i<len(tab[p])):
          ln += "| {:7}: {:4}".format(tab[p][i], ctab[ty][i])
      print(ln)
      ty += 1

  else:
    for p in tab:
      ln = "{:10} ".format(p)
      for i in range(0,len(tab[p])):
        ln += "| {:7}: {:4}".format(tab[p][i], ctab[ty][i])
        if i==6:
          print(ln)
          ln = "{:20}".format("     ->")
      print(ln)
      ty += 1
  print("=========================================================================")

elif fmt=='list':
  ty = 0;
  print("~~~~~ List of FP instructions ~~~~~")
  for p in tab:
    print("|> {:10} ".format(p))
    for i in range(0,len(tab[p])):
      ln = "| {:7}: {:4}".format(tab[p][i], ctab[ty][i])
      print(ln)
    ty += 1
  print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")