inspectfp_rv.py 4.5 KB
#!/usr/bin/python

# inspectfp.py <input_file>

import os
import sys

# search the following instructions in the input file
tab = {"double"     : ["fld", "fsd", "fmadd.d", "fmsub.d", "fnmsub.d", "fnmadd.d", "fadd.d", "fsub.d", "fmul.d", "fdiv.d", "fsqrt.d", "fsgnj.d", "fsgnjn.d", "fsgnjx.d", "fmin.d", "fmax.d", "fcvt.s.d", "fcvt.d.s", "feq.d", "flt.d", "fle.d", "fclass.d", "fcvt.w.d", "fcvt.wu.d", "fcvt.d.w", "fcvt.d.wu", "fcvt.l.d", "fcvt.lu.d", "fcvt.d.l", "fcvt.d.lu", "fmv.x.d", "fmv.d.x"],
       "single"     : ["flw", "fsw", "fmadd.s", "fmsub.s", "fnmsub.s", "fnmsub.s", "fadd.s", "fsub.s", "fmul.s", "fdiv.s", "fsqrt.s", "fsgnj.s", "fsgnjn.s", "fsgnjx.s", "fmin.s", "fmax.s", "feq.s", "flt.s", "fle.s", "fclass.s", "fcvt.w.s", "fcvt.wu.s", "fcvt.s.w", "fcvt.s.wu", "fcvt.l.s", "fcvt.lu.s", "fcvt.s.l", "fcvt.s.lu", "fmv.x.w", "fmv.w.x"],
       "half"       : ["flh", "fsh", "fmadd.h", "fmsub.h", "fnmsub.h", "fnmadd.h", "fadd.h", "fsub.h", "fmul.h", "fdiv.h", "fsqrt.h", "fsgnj.h", "fsgnjn.h", "fsgnjx.h", "fmin.h", "fmax.h", "fcvt.s.h", "fcvt.d.h", "fcvt.h.s", "fcvt.h.d", "feq.h", "flt.h", "fle.h", "fclass.h", "fcvt.w.h", "fcvt.wu.h", "fcvt.h.w", "fcvt.h.wu", "fmv.x.h", "fmv.h.x", "fcvt.l.h", "fcvt.lu.h", "fcvt.h.l", "fcvt.h.lu"],
# flph,fsph,fmv.x.ph,fmv.ph.x are alieses to flw,fsw,fmv.x.w,fmv.w.x
       "packhalf"   : ["flph", "fsph", "fmv.x.ph", "fmv.ph.x", "fsgnj.ph", "fsgnjn.ph", "fsgnjx.ph", "fclass.ph", "feq.ph", "flt.ph", "fle.ph", "fadd.ph","fsub.ph","fmul.ph","fdiv.ph","fsqrt.ph","fmin.ph","fmax.ph","faddx.ph","fsubx.ph","fmulx.ph","fdiv.ph","faddr.ph","fsubr.ph","fmulr.ph","fdivr.ph","faddsubr.ph","fsubaddr.ph","fswap.ph","fmvuu.ph","fmvll.ph","fmvul.ph","fmvlu.ph","fmvzu.ph","fmvzl.ph"],
# flps,fsps,fmv.x.ps,fmv.ps.x are alieses to fld,fsd,fmv.x.d,fmv.d.x
       "packsingle" : ["flps", "fsps", "fmv.x.ps", "fmv.ps.x", "fsgnj.ps", "fsgnjn.ps", "fsgnjx.ps", "fclass.ps", "feq.ps", "flt.ps", "fle.ps", "fadd.ps","fsub.ps","fmul.ps","fdiv.ps","fsqrt.ps","fmin.ps","fmax.ps","faddx.ps","fsubx.ps","fmulx.ps","fdiv.ps","faddr.ps","fsubr.ps","fmulr.ps","fdivr.ps","faddsubr.ps","fsubaddr.ps","fswap.ps","fmvuu.ps","fmvll.ps","fmvul.ps","fmvlu.ps","fmvzu.ps","fmvzl.ps"],
#       "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
        ipos = ln.find("\t"+tab[p][i]+"\t")
        if ipos>0:
          if p == "others":
            if tab[p][i].startswith("st"):
              lnarg = ln[ipos+len(tab[p][i]):].strip()
              if lnarg.startswith("%f"):
                ctab[ty][i] += 1
            elif tab[p][i].startswith("ld"):
              lnarg = ln[ipos+len(tab[p][i]):].replace(" ","")
              if lnarg.find(",%f")>0:
                ctab[ty][i] += 1
          else:
            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("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")